bryniek
bryniek

Reputation: 141

repeating specific characters in string java

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);

Upvotes: 0

Views: 3297

Answers (4)

Johny
Johny

Reputation: 2188

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);

Upvotes: 0

kattoor
kattoor

Reputation: 195

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());

Upvotes: 0

Rock Man
Rock Man

Reputation: 18

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);

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60045

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

Upvotes: 2

Related Questions