Answer to Question #294978 in Java | JSP | JSF for Pankaj

Question #294978

 Write a program to count the number of occurrences of each character in a string and store that number in an array.{java code}


1
Expert's answer
2022-02-07T15:40:09-0500
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();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS