Write a java program to find Repeating set of characters in a string.

Get a string and a positive integer n as input. The last n characters should repeat the number of times given as second input.Write code to repeat the set of character from the given string.

The return type of the output is a string with repeated n characters.

Input and Output Format:

Input consists a string and a positive integer n.

Output is a string with repeated characters.

Refer sample output for formatting specifications.

 

Sample Input 1:

codebun

3

Sample Output 1:

codebunbunbunbun

Sample Input 2:

java

2

Sample Output 2:

javavava

Find the repeating set of characters in a string in Java.

import java.util.*;
public class Main {
                public static String lengthiestString(String s1,int n){
                                StringBuffer sb=new StringBuffer();
                                sb.append(s1);
                                for(int i=0;i<n;i++)
                               
                                {
                                               
                                                sb.append(s1.substring(s1.length()-n,s1.length()));
                                               
                }
        return sb.toString();
                }
                public static void main(String[] args) {
                                Scanner s=new Scanner(System.in);
                                System.out.println("enter the String:");
                                String s1=s.nextLine();
                                int n=s.nextInt();
                                System.out.println("the lengthiest string is:"+lengthiestString(s1,n));
                                }
}

Add a Comment

Your email address will not be published. Required fields are marked *

twelve − three =