0% found this document useful (0 votes)
17 views

Test String

The code defines a method to count the number of occurrences of a character in a string. It then prompts the user to enter a string, loops through each character, counts the occurrences of that character, and prints the results.

Uploaded by

Mr. X
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Test String

The code defines a method to count the number of occurrences of a character in a string. It then prompts the user to enter a string, loops through each character, counts the occurrences of that character, and prints the results.

Uploaded by

Mr. X
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;

public class TestString {


public static int Find(String string, char key) {
int count = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == key) {
count++;
}
}
return count;
}

public static void main(String[] args) {


final Scanner input = new Scanner(System.in);

System.out.print("String :");
String string = input.next();
input.close();

for (int i = 0; i < string.length(); i++) {


char key = 'a';
char[] count = new char[string.length()];
int pass = 0;
if (string.charAt(i) <= 'z' && string.charAt(i) >= 'a'
|| string.charAt(i) <= 'Z' && string.charAt(i) >= 'A') {
key = string.charAt(i);
count[i] = key;
}
for (int j = 0; j < string.length(); j++) {
if (string.charAt(i) == count[i]) {
pass++;
}
if (pass == 1) {
System.out.println(
"There are " + Find(string, key) + " " + key + "(s)
appears in " + "\"" + string + "\"");
}
}
}
}
}

You might also like