Write a program to count the number of occurrences of each character in a string and store that number in an array.{java code}
import java.util.*;
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a String: ");
String inputString = in.nextLine();
int count[] = new int[256];
int length = inputString.length();
// Initialize count array index
for (int i = 0; i < length; i++) {
count[inputString.charAt(i)]++;
}
char letters[] = new char[inputString.length()];
for (int i = 0; i < length; i++) {
letters[i] = inputString.charAt(i);
int index = 0;
for (int j = 0; j <= i; j++) {
if (inputString.charAt(i) == letters[j]) {
index++;
}
}
if (index == 1) {
System.out.println(
"Number of Occurrence of " + inputString.charAt(i) + " is: " + count[inputString.charAt(i)]);
}
}
in.close();
}
}
Comments
Leave a comment