0

I'm currently trying to loop through a String and identity a specific character within that string then add a specific character following on from the originally identified character.

For example using the string: aaaabbbcbbcbb And the character I want to identify being: c

So every time a c is detected a following c will be added to the string and the loop will continue.

Thus aaaabbbcbbcbb will become aaaabbbccbbccbb.

I've been trying to make use of indexOf(),substring and charAt() but I'm currently either overriding other characters with a c or only detecting one c.

1
  • 6
    Kindly share what you have tried so far. Commented Oct 24, 2018 at 11:20

4 Answers 4

3

I know you've asked for a loop, but won't something as simple as a replace suffice?

String inputString = "aaaabbbcbbcbb";
String charToDouble = "c";

String result = inputString.replace(charToDouble, charToDouble+charToDouble);
// or `charToDouble+charToDouble` could be `charToDouble.repeat(2)` in JDK 11+

Try it online.

If you insist on using a loop however:

String inputString = "aaaabbbcbbcbb";
char charToDouble = 'c';

String result = "";
for(char c : inputString.toCharArray()){
  result += c;
  if(c == charToDouble){
    result += c;
  }
}

Try it online.

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

1 Comment

A much more simple solution! I had never come across .replace() thank you very much.
1

Iterate over all the characters. Add each one to a StringBuilder. If it matches the character you're looking for then add it again.

final String test = "aaaabbbcbbcbb";
final char searchChar = 'c';

final StringBuilder builder = new StringBuilder();
for (final char c : test.toCharArray())
{
    builder.append(c);
    if (c == searchChar)
    {
        builder.append(c);
    }
}
System.out.println(builder.toString());

Output

aaaabbbccbbccbb

Comments

1

You probably are trying to modify a String in java. Strings in Java are immutable and cannot be changed like one might do in c++.

You can use StringBuilder to insert characters. eg:

StringBuilder builder = new StringBuilder("acb");
builder.insert(1, 'c');

2 Comments

This just adds a 'c' between the 1st and 2nd characters. That's not what they asked for.
Inserting an extra c when you find a c could be one approach to solve his problem. @Michael
0

The previous answer suggesting String.replace is the best solution, but if you need to do it some other way (e.g. for an exercise), then here's a 'modern' solution:

public static void main(String[] args) {
    final String inputString = "aaaabbbcbbcbb";
    final int charToDouble = 'c';  // A Unicode codepoint
    final String result = inputString.codePoints()
            .flatMap(c -> c == charToDouble ? IntStream.of(c, c) : IntStream.of(c))
            .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
            .toString();
    assert result.equals("aaaabbbccbbccbb");
}

This looks at each character in turn (in an IntStream). It doubles the character if it matches the target. It then accumulates each character in a StringBuilder.

A micro-optimization can be made to pre-allocate the StringBuilder's capacity. We know the maximum possible size of the new string is double the old string, so StringBuilder::new can be replaced by () -> new StringBuilder(inputString.length()*2). However, I'm not sure if it's worth the sacrifice in readability.

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.