Home > Mobile >  Doubling one letter with each new occurence
Doubling one letter with each new occurence

Time:05-20

so I have task to double number of letter "a" every time it occurs in a string. For example sentence "a cat walked on the road" , at the end must be "aa caaaat waaaaaaaalked on the roaaaaaaaaaaaaaaaa" . I had something like this on my mind but it doubles every charachter, not only "a".

    public static void main(String[] args) {
    String s = "a bear walked on the road";
    String result = "";
    int i = 0;
    while(i<s.length()){
        char a = s.charAt(i);
        result = result   a   a;
        i  ;
    }
    System.out.println(result);
}

CodePudding user response:

You need to check what the char a is (in your case, 'a'). Additionally, you do not repeat the characters more than twice in your code, hence not getting the answer you expected: result = result a a only adds 'a' twice, not giving you: "aa caaaat waaaaaaaalked...".

Here is the solution:

public static void main(String[] args) {
    String s = "a bear walked on the road";
    String result = "";
    char lookingFor = 'a'; // Can change this to whatever is needed
    int counter = 2;
    for (int i = 0; i < s.length(); i  ) {
        if (s.charAt(i) == lookingFor) { // The current character is what we need to be repeated.
            
            // Repeat the character as many times as counter is (each loop: 2, 4, 6, 8, ...)
            for (int j = 0; j < counter; j  ) {
                result  = lookingFor;
            }

            counter *= 2; // Double counter at every instance of 'a'
        }
        else { // The current char is not what we are looking for, so we just add it to our result.
            result  = s.charAt(i);
        }
    }
    System.out.println(result);
}

CodePudding user response:

Try this out:

public static void main(String[] args) {
    char letterToSearch = 'a'
    String myString = "a bear walked on the road";
    StringBuilder result = new StringBuilder();
    int occurrance = 0;
    for (int i = 0; i < myString.length(); i  ){
        char currChar = myString.charAt(i);
        if (currChar == letterToSearch){
            occurrance =1;
            for (int j = 0; j < 2*occurrance; j  ){
                result.append(currChar);
            }
        } else {
            result.append(currChar);
        }
    }
    System.out.println(result);
}

The variable occurrance keeps track of how many as you have.

CodePudding user response:

The problems are:

  • you are doubling every character because you are not testing if it is an 'a' or not.
  • and you are not doubling the substitution each time.

Here is a modified version of your solution.

String s = "a bear walked on the road";
String result = "";
String sub = "aa";
int i = 0;
while(i<s.length()){
    // get the character
    char ch = s.charAt(i  );
    //if it an a, append sub to result
    // and double sub.
    if (ch == 'a') {
          result  =  sub;
          sub  = sub;
    } else {
        // otherwise, just append the character
        result  = ch;
    }
}

Here is another way.

  • check each character and double the replacement each time an a is encountered.
String str = "a cat walked on the road";
StringBuilder sb = new StringBuilder();
String sub = "a";
for (String s : str.split("")) {
    sb.append(s.equals("a") ? (sub  = sub) : s);
}
        
System.out.println(sb);

prints

aa caaaat waaaaaaaalked on the roaaaaaaaaaaaaaaaad
  • Related