Java String Character Repetition Count
Chapter:
Interview Programs
Last Updated:
15-05-2017 15:28:25 UTC
Program:
/* ............... START ............... */
public class JavaStringCharacterRepetationCount {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the String");
String str = in.nextLine();
int count = 0;
String temp = "";
for (int i = 0; i < str.length(); i++) {
char c1 = str.charAt(i);
for (int j = i; j < str.length(); j++) {
char c2 = str.charAt(j);
if (c1 == c2 && temp.indexOf(c1) == -1) {
count = count + 1;
}
}
if (temp.indexOf(c1) == -1) {
temp = temp + c1;
System.out.println("The Character " + c1 + " "
+ "has occurred " + count + " times");
}
count = 0;
}
}
}
/* ............... END ............... */
Output
Enter the String
JavaScan.com
The Character J has occurred 1 times
The Character a has occurred 3 times
The Character v has occurred 1 times
The Character S has occurred 1 times
The Character c has occurred 2 times
The Character n has occurred 1 times
The Character . has occurred 1 times
The Character o has occurred 1 times
The Character m has occurred 1 times
Tags
String Character Repetition Count, Java, Interview Programs