Using java language, Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.
Using java language, Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.
import java.util.Scanner;
public class CharCountp {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char userChar = scan.next().charAt(0);
String userString = scan.nextLine();
int count = 0;
for(int i = 0;i<userString.length();i++){
if(userString.charAt(i)==userChar){
count += 1;
}
}
System.out.println(count);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps