Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Shavit Borisov
- // CW
- import java.util.Scanner;
- public class StringAndChar {
- public static void main(String[] args)
- {
- Scanner in = new Scanner (System.in);
- String str;
- char ch;
- int counter = 0;
- System.out.print("Enter a string: ");
- str = in.nextLine();
- System.out.print("Enter a character you want to check for: ");
- ch = in.next().charAt(0);
- for(int i = 0; i < str.length(); i++)
- if(str.charAt(i) == ch)
- counter++;
- System.out.printf("The number of times '%c' appears in your string is %d", ch, counter);
- in.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement