Advertisement
Shavit

P. 6 Ex. 9.2

Dec 24th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class StringAndChar {
  7.  
  8.     public static void main(String[] args)
  9.    
  10.     {
  11.         Scanner in = new Scanner (System.in);
  12.        
  13.         String str;
  14.         char ch;
  15.         int counter = 0;
  16.        
  17.         System.out.print("Enter a string: ");
  18.         str = in.nextLine();
  19.        
  20.         System.out.print("Enter a character you want to check for: ");
  21.         ch = in.next().charAt(0);
  22.        
  23.         for(int i = 0; i < str.length(); i++)
  24.             if(str.charAt(i) == ch)
  25.                 counter++;
  26.        
  27.         System.out.printf("The number of times '%c' appears in your string is %d", ch, counter);
  28.        
  29.         in.close();
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement