Results 1 to 6 of 6

Thread: String Exercise

  1. #1
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default String Exercise

    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.)

    countYZ("fez day") → 2
    countYZ("day fez") → 2
    countYZ("day fyyyz") → 2




    The problem says two loops. Here's my algorithm but I dont know how to implement it (im not so good with the character/string class):

    Look for a space using indexOf(' '). Once you find the space, go back one index and see if it is a y or z... if it is.. raise the globalCount once.

    Look at the end of the string using the .length() method. If the end of the string is y or z... add the global count.

    Can anyone show me the light?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    your best bet is to give it a try first and try to implement the code yourself, then come back with this code if it doesn't work. Just throwing in the towel without first putting in some effort will guarantee that you'll never learn Java.

  3. #3
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    Hi. I wanted to know if my logic is correct first before I coded. Is there a better algorithm than the one that I said?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    seriously man, the best test of your logic is to code it and run test sentences through it.

  5. #5
    jithan is offline Member
    Join Date
    Jun 2008
    Posts
    39
    Rep Power
    0

    Default

    class CountChars
    {
    public static void main(String arg[])
    {
    String str = "day fyyyz";
    String sp[] = str.split(" ");
    int count = 0;
    for(int i=0;i<sp.length;i++)
    {
    if(sp[i].endsWith("z") || sp[i].endsWith("y"))
    count++;
    }
    System.out.println(" Count chars ========= " + count);
    }
    }

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    jithan... a couple of things... it's not a good idea to post complete code to OPs... too much temptation to cut & paste & forget. There's no learning process.
    Another thing is that the code that you posted doesn't use the assignment specifications. You can get the OP in trouble ...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Help with Beginner exercise.
    By Trunksten in forum New To Java
    Replies: 1
    Last Post: 07-10-2008, 05:40 AM
  2. Prob with an exercise
    By jhetfield18 in forum New To Java
    Replies: 4
    Last Post: 02-15-2008, 06:11 PM
  3. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 04:48 PM
  4. help with exercise
    By e_as're in forum New To Java
    Replies: 3
    Last Post: 09-25-2007, 10:14 AM
  5. help with an exercise calcuting tax
    By e_as're in forum New To Java
    Replies: 7
    Last Post: 08-01-2007, 03:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •