Write Java code to count the number of characters that get repeated 3 times consecutively and return that count (ignore case). Get a string as input. If no character gets repeated 3 times consecutively return -1. The return type of the output is the repeat count.
Input and Output Format
- Input consists of a string.
- The output is an integer.
Refer sample output for formatting specifications
Sample Input 1:
abcXXXabc
Sample Output 1:
1
Sample Input 2:
aaaxxyzAAAx
Sample Output 2:
2
Count Sequential Characters in Java
Following are the steps to count sequential characters in Java:
- Input a string.
- Call the consecutiveRepeatingOfChar() method with the input string.
- Inside the method, declare and initialize variables c and n with 0.
- Iterate over the string, and check for the following condition
- If the character at ith position is equal to the i+1 position then increment n value.
- Otherwise, set n to 0.
- Next, if the value of n is 2 then increment the value of count in the c variable.
- At last, return value of count i.e c variable.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input1 = sc.nextLine();
System.out.println(consecutiveRepeatitionOfChar(input1));
}
public static int consecutiveRepeatitionOfChar(String input1) {
int c = 0;
int n = 0;
for (int i = 0; i < input1.length() - 1; i++) {
if (input1.charAt(i) == input1.charAt(i + 1))
n++;
else
n = 0;
if (n == 2)
c++;
}
return c;
}
}
Output
Count Frequency of characters in Java
Write a program in Java to count the number of frequency of characters in the string. Let ‘aaabbbcc’ be a string and the count of characters will be ‘a3b3c2’.
Input and Output Format
- Input consists of a string.
- The output of a string with an integer value.
Refer sample output for formatting specifications
Sample Input 1:
aaaXXXcc
Sample Output 1:
a3 X3 c2
Program to count frequency of characters in Java
Following is the step we will follow to count the count frequency of characters:
- Input a string from the user.
- Iterate over the string and for each iteration increment count value.
- Repeat this process till the character at ith position matches the next character.
- At last print that character with its count.
package com.company;
import java.util.Scanner;
public class Practice11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
int count;
for(int i = 0;i<str.length();i++){
count = 1;
while(i+1<str.length() && str.charAt(i)==str.charAt(i+1)){
i++;
count++;
}
System.out.print(str.charAt(i)+""+count+" ");
}
}
}
Output
Count of Occurrence of each character using HashMap
Write a program in Java to count the number of occurrences of characters in the string using HashMap. Let ‘aaabbbcc’ be a string and the count of characters will be ‘a3b3c2’.
Input and Output Format
- Input consists of a string.
- The output of a string with an integer value.
Refer sample output for formatting specifications
Sample Input 1:
aaaXXXccc
Sample Output 1:
a3 X3 c3
Java Program to Count of Occurrence of each character
Following are the steps to find the court of occurrence of each character in string in Java:
- Input a string from the user.
- Declare a HashMap of Character and Integer.
- Convert string to character array using toCharArray() method.
- Iterate over the HashMap and check for the following condition
- If traverse character is present increment map’s value to 1.
- Otherwise, Add 1 to its map value.
- At last, print the character and its count of value by traversing the HashMap.
package com.company;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Practice12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
HashMap<Character,Integer> map = new HashMap<>();
char c[] = str.toCharArray();
for(char char1:c){
if(map.containsKey(char1))
map.put(char1,map.get(char1)+1);
else
map.put(char1,1);
}
for(Map.Entry entry: map.entrySet()){
System.out.print(entry.getKey()+""+entry.getValue()+" ");
}
}
}
Output
Thus, in this way, we learn How to find the count of sequential characters along with finding the frequency of characters in a given string with HashMap and without HashMap.