write a program to count repeat character from a string?

In this program we use for loop where convert String to character.we compare one character to last character.if it is equal then increase count.

				
					public class RepeatChar {
	public static int test(String s) {
		int count = 0;
		char last = 0;
		for (char ch : s.toCharArray()) {
			if (ch == last) {
				count++;
			}
			last = ch;
		}
		return count;
	}

	public static void main(String[] args) {
		String s = "PPMMDD";
		System.out.println(" Repeat count char is-- " + test(s));
	}
}
				
			

Output :-
Repeat count char is– 3

Leave a Comment

Your email address will not be published. Required fields are marked *