Skip to content
Advertisement

Code that takes a string and recognizes the number of consecutive letters

To do my job, I need a code that takes a word from the user, then recognizes the number of consecutive letters and outputs it in such a way that it prints the letter and the number of times it is repeated.

Example 1 input:

hhhttrew

Example 1 output:

h3t2rew

Example 2 input:

uuuuuuhhhaaajqqq

Example 2 output:

u6h3a3jq3
String text = sc.nextLine();
            int len = text.length();

            int repeat = 0;

            char[] chars = new char[len];

            // To convert string to char
            for (int h = 0; h < len; h++)
            {
                chars[h] = text.charAt(h);
            }

            String finaly = "";

            for (char ignored : chars)
            {
                for (int j = 0 ; j <len ; j++ )
                {
                    if (chars[j] == chars[j+1])
                    {
                        finaly = String.valueOf(chars[j]);

                        repeat++;

                        finaly = String.valueOf(repeat);
                    }
                    else
                    {
                        j++;
                    }
                }
            }
            System.out.println(finaly);

Advertisement

Answer

Here is one way. You only need a single loop. The inner loop does the work. The outer loop simply supplies test cases.

  • assign the first character
  • and set count to 1 for that character
  • then iterate until adjacent characters are different
  • append count if > 1 and append the different character
  • set count to 0 for next run.
String[] data = { "uuuuuuhhhaaajqqq", 
        "hhhttrew","abbcccddddeeeeeffffffggggggg" };

for (String s : data) {
    String result = "" + s.charAt(0);
    int count = 1;
    for (int i = 1; i < s.length(); i++) {
        if (s.charAt(i - 1) != s.charAt(i)) {
            result += count <= 1 ? "" : count;
            result += s.charAt(i);
            count = 0;
        }
        count++;
        if (i == s.length() - 1) {
            result += count <= 1 ? "" : count;
        }
    }
    System.out.printf("%-15s <-- %s%n", result, s);
}

prints

u6h3a3jq3       <-- uuuuuuhhhaaajqqq
h3t2rew         <-- hhhttrew
ab2c3d4e5f6g7   <-- abbcccddddeeeeeffffffggggggg

In a comment (now deleted) you had enquired how to reverse the process. This is one way to do it.

  • allocate a StringBuilder to hold the result.
  • initialize count and currentChar
  • as the string is processed,
    • save a character to currentChar
    • then while the next char(s) is a digit, build the count
  • if the count is still 0, then the next character was a digit so bump count by one and copy the currentChar to the buffer
  • otherwise, use the computed length.
String[] encoded =
        { "u6h3a3jq3", "h3t2rew", "ab2c3d4e5f6g7" };

for (String s : encoded) {
    
    StringBuilder sb = new StringBuilder();
    int count = 0;
    char currentChar = '';
    for (int i = 0; i < s.length();) {
        if (Character.isLetter(s.charAt(i))) {
            currentChar = s.charAt(i++);
        }
        while (i < s.length()
                && Character.isDigit(s.charAt(i))) {
            count = count * 10 + s.charAt(i++) - '0';
        }
        count = count == 0 ? 1 : count;
        sb.append(Character.toString(currentChar)
                .repeat(count));
        count = 0;
    }
    System.out.println(s + " --> " + sb);
}

prints

u6h3a3jq3 --> uuuuuuhhhaaajqqq
h3t2rew --> hhhttrew
ab2c3d4e5f6g7 --> abbcccddddeeeeeffffffggggggg
Advertisement