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