Advertisement
Slyke

W233

Jul 28th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import java.util.*; public class W233 {public static void main (String[] args) {while (JPL.test()) {
  2.  
  3. ////////////////////////////// PROBLEM STATEMENT //////////////////////////////
  4. // Given a string, count the number of words ending in 'y' or 'z' -- so the  //
  5. // 'y' in "heavy" and the 'z' in "fez" count, but not the 'y' in "yellow"    //
  6. // (not case sensitive). We'll say that a y or z is at the end of a word if  //
  7. // there is not an alphabetic letter immediately following it. (Note:        //
  8. // Character.isLetter(char) tests if a char is an alphabetic letter.)        //
  9. //   "fez day" -> 2                                                          //
  10. //   "day fez" -> 2                                                          //
  11. //   "day fyyyz" -> 2                                                        //
  12. ///////////////////////////////////////////////////////////////////////////////
  13.  
  14.   // >>>>>> Your Java Code Fragment starts here <<<<<<
  15.  
  16.   Scanner kb=new Scanner(System.in);
  17.  
  18.   String theInput=kb.nextLine().toLowerCase();
  19.   int strLength=theInput.length();
  20.   int count=0;
  21.  
  22.   for (int i=1;i<strLength;i++) {
  23.     if (theInput.charAt(i)==' ' || theInput.charAt(i)=='-' || theInput.charAt(i)==':' || theInput.charAt(i)=='!' || theInput.charAt(i)=='2') {
  24.       if ((theInput.charAt(i-1)=='y') || (theInput.charAt(i-1)=='z')) {
  25.         count++;
  26.       }
  27.     }
  28.   }
  29.  
  30.   if ((theInput.endsWith("y")) || (theInput.endsWith("z"))) {
  31.     count++;
  32.   }
  33.  
  34.   System.out.print(count);
  35.  
  36.   // >>>>>> Your Java Code Fragment ends here <<<<<<
  37. }}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement