0

I have a question. I'm struggling with a exercise where I am asked to get from user a string, a character which I want to duplicate in this string, and a number - how many times I want to duplicate. Eg: string input: dog; character: o; number:4. Output: doooog. My question is how can I achieve this result?

Scanner sc = new Scanner(System.in);

System.out.println("enter your string");
String text = sc.nextLine();

System.out.println("enter the character that will be repeated");
char character= sc.next().charAt(0);

System.out.println("enter the number of repetitions");
int repetitions = sc.nextInt();

for (int i = 0; i < text.length(); i++) {
    char z = text.charAt(i);
    if(z==character) {
         // repeat character in string put by user * repetitions 
    }
}
System.out.println(text);
0

4 Answers 4

2

If you are using Java 8, you can use String.join and replace like this :

String str = "dog";
int length = 4;
String s = "o";
str = str.replace(s, String.join("", Collections.nCopies(length, s)));// doooog

read more about Collections::nCopies

Sign up to request clarification or add additional context in comments.

Comments

0

To complete your code, insert the following:

String ans="";
for (int i = 0; i < text.length(); i++) {
    char z = text.charAt(i);
    if(z==character) {
         for(int j=0;j<repetitions;j++)
            ans+=z;
    }
    else
        ans+=z;
}

However, there is a far more elegant way of doing this:

String replacement="";
for(int i=0;i<repetitions;i++)
    replacement+=character;
String ans = text.replaceAll(String.valueOf(character), replacement);

Comments

0

The below should work:

StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < text.length(); i++) {
                    char z = text.charAt(i);
                    if(z==character) {
                         // repeat character in string put by user * repetitions 
                        for (int j=1;j<=repetitions;j++){
                            buffer.append(z);
                        }
                    }else{
                         buffer.append(z);
                    }
                }
System.out.println(buffer.toString());

Comments

0

If you are not using Java 8 you can try this.

StringBuilder builder = new StringBuilder();
String repetitionString = String.format("%" + repetitions + "s", character).replace(' ', character);
for (int i = 0; i < text.length(); i++) {
    char z = text.charAt(i);
    builder.append(z == character ? repetitionString : z);
}

text = builder.toString();
System.out.println(text);

In this solution we are using String.format for repeating the character that we want instead of a loop.

String.format("%" + repetitions + "s", z).replace(' ', z)

This is similar to what we can do in Python using the * operator, ie

z * repeatitions

which will repeat the character in z required number of times.

Another approach by which you can avoid the loops is using String.replace() method.

String repetitionString = String.format("%" + repetitions + "s", character).replace(' ', character);
text = text.replace(String.valueOf(character), repetitionString);
System.out.println(text);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.