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:
- Array square
- Generate number using odd digits
- Alternate Number Difference
- Next Greatest number
- Mark Comparison
- Print the characters in descending order
- Vowels in a fishBowl
- Least offer
- Ascending and descending order
- Mail Domain
- Count repeating words
- Sentence – Convert to upper and lower
- Count consecutive repeating characters
- Zig zag Array
- Pass and Fail Count
- Search a Course
- Average and Grade Calculation
- String – Find and replace the character (first occurrence)
- Sort the first and second half of an array
- Retail Shop
- Palindrome
- Numerology
- InitCap
- Array Compatiblilty
- Sum of the maximum and the minimum element
- String Concatenation
- Find Average Age
- Login