Java Program of Count consecutive repeating characters

Question:-

Write a program to count the consecutive repeating characters.

Sample Input1:

aaabbcbbbb

Sample Output1:

a3b2c1b4

Sample Input2:

aaaabbcbbbb

Sample Output2:

a4b2c1b4

Sample Input3:

ascbnt

Sample Output2:

ascbnt

Code:-

Main.java

import java.util.Scanner;
public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        char[] char_arr = input.toCharArray();
        int count, i = 0;
        boolean cons = false;
        while(i <= char_arr.length - 1){
            count = 1;
            System.out.print(char_arr[i]);
            if(i == char_arr.length - 1)
                break;
            while(char_arr[i] == char_arr[i+1]){
                count++;
                cons = true;
                i++;
                if(i == char_arr.length - 1)
                    break;
            }
            i++;
            if(cons == true)
                System.out.print(count);
        }
    }
}

Recommended:

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

Leave a Comment

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