Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*; public class W233 {public static void main (String[] args) {while (JPL.test()) {
- ////////////////////////////// PROBLEM STATEMENT //////////////////////////////
- // Given a string, count the number of words ending in 'y' or 'z' -- so the //
- // 'y' in "heavy" and the 'z' in "fez" count, but not the 'y' in "yellow" //
- // (not case sensitive). We'll say that a y or z is at the end of a word if //
- // there is not an alphabetic letter immediately following it. (Note: //
- // Character.isLetter(char) tests if a char is an alphabetic letter.) //
- // "fez day" -> 2 //
- // "day fez" -> 2 //
- // "day fyyyz" -> 2 //
- ///////////////////////////////////////////////////////////////////////////////
- // >>>>>> Your Java Code Fragment starts here <<<<<<
- Scanner kb=new Scanner(System.in);
- String theInput=kb.nextLine().toLowerCase();
- int strLength=theInput.length();
- int count=0;
- for (int i=1;i<strLength;i++) {
- if (theInput.charAt(i)==' ' || theInput.charAt(i)=='-' || theInput.charAt(i)==':' || theInput.charAt(i)=='!' || theInput.charAt(i)=='2') {
- if ((theInput.charAt(i-1)=='y') || (theInput.charAt(i-1)=='z')) {
- count++;
- }
- }
- }
- if ((theInput.endsWith("y")) || (theInput.endsWith("z"))) {
- count++;
- }
- System.out.print(count);
- // >>>>>> Your Java Code Fragment ends here <<<<<<
- }}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement