In a given string find the repetitive characters. For example for Input: aaaabbbccaaabbc output should be a4b3c2a3b2c1

Below approach is using StringBuffer there can be many other ways:

package testjava.strings;

public class LetterRepeat 
{
	public static void main(String args[])
	{
		String stringInput = "aaaabbbccaaabbc";
		char[] charInput = stringInput.toLowerCase().toCharArray();
		StringBuffer sb = new StringBuffer();
		
		for(int i = 0, count; i < charInput.length; i += count)
		{
			count = 1;
			while(i + count < charInput.length && charInput[i] == charInput[i + count])
			{
				count ++;
			}
			sb.append(charInput[i]).append(count);
		}
		String result = sb.toString();
		System.out.println("Input: "+stringInput);
		System.out.println("Output: "+result);
	}	
}

Output: 

Input: aaaabbbccaaabbc

Output: a4b3c2a3b2c1