Advertisement
Shavit

P. 39 Ex. 10.18

Dec 31st, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class StringLastTwoCharacters {
  7.  
  8.     public static void main(String[] args)
  9.    
  10.     {
  11.         Scanner in = new Scanner (System.in);
  12.        
  13.         final int STRING_SIZE = 20;
  14.         String input;
  15.         int counter = 0;
  16.        
  17.         System.out.printf("Enter a string, %d characters long:\n", STRING_SIZE);
  18.         input = in.nextLine();
  19.        
  20.         char one = input.charAt(STRING_SIZE - 2);
  21.         char two = input.charAt(STRING_SIZE - 1);
  22.        
  23.         for(int i = 0; i < STRING_SIZE - 2; i++)
  24.             if((input.charAt(i) == one) && (input.charAt(i + 1) == two))
  25.                 counter++;
  26.        
  27.         System.out.printf("The number of '%c%c' pairs is %d.", one, two, counter);
  28.        
  29.         in.close();
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement