Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Making nouns plural

    I have an assignment for a class that I am not sure how to approach. A string, a noun, is input and the program is supposed to make it plural based on the ending. If it ends in a double consonant then I add one ending, if it ends in a vowel I add a different one and then if it ends in two vowels or two consonants I add a third, different ending. I am supposed to do this using string methods. For the project the vowels are A, C, L, and S. I have to read the data in using the string tokenizer.

    I am not sure how to approach making the loops I need to use to make the words plural. Can anyone help??

    EDIT: I have to use if statements to do this.
    Last edited by kcc989; 12-14-2009 at 12:12 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    String Tokenizer indicates to me that this is supposed to be Java, not Javascript. I'll request this to be moved to the Java forum.
    You don't really need to do much by way of looping in this assignment aside from the token retrieving. All you need to do to loop a string tokenizer is to check if st.hasMoreTokens(). While thats true, it will loop. Retrieve each with nextToken().
    Inside of this loop you simply want to check for the last chars. Work it from shortest to longest case, or use a substring to pull out the last few chars. Its up to you to handle those.

    That help? Can't give too much information since this is an assignment and its considered plagiarism if you just get the code from here.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Sep 2008
    Posts
    48
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Based on the ending, you need to add a certain ending to the variable? I'd do it like this (where ip is the input):
    Code:
    var split = ip.split("")
    //splits the word into its individual letters
    var ending = split[ip.length-3] + split[ip.length-2] + split[ip.length-1]
    //ending will contain the last 3 letters of the given noun
    After that code, you can just have a series of if statements to check for each type of ending, and pluralize them accordingly

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by blitz104 View Post
    Based on the ending, you need to add a certain ending to the variable? I'd do it like this (where ip is the input):
    Code:
    var split = ip.split("")
    //splits the word into its individual letters
    var ending = split[ip.length-3] + split[ip.length-2] + split[ip.length-1]
    //ending will contain the last 3 letters of the given noun
    After that code, you can just have a series of if statements to check for each type of ending, and pluralize them accordingly
    I can't split the code into individual letters because in my class we haven't covered that yet. Anyway I am struggling on using the statements to check for endings. This is the loop I am using right now, that isn't working.

    Code:
    public static void makePlural()
      	{
      		for (int i = 3; i < word.length();) {
            char c = word.charAt(i);
            if (c=='A' || c=='C' || c=='L' || c=='S') {output =
            	word + "G" ;}
            else {output = word + "GH";
            }
                    }
      	}
    And yes I put this in the wrong forum, my bad.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    blitz's code is for Javascript, to split them.
    Using charAt(x) would be my suggestion over substring simply since you don't need to keep the string - may as well save the memory.
    The loop itself is a problem since you're not incrementing it anywhere, so it will infinitely loop.
    This loop would start at the third fourth character in the string, and search from there. Thats not what you want, what you want is to search from the end of the loop. You can do this without the loop:
    PHP Code:

    int i 
    word.length() - 1;
    switch(
    word.charAt(i))
    {
        case 
    'A':
        case 
    'C':
        case 
    'L':
        case 
    'S':
            
    output word "G";
            break;
        default:
            
    output word "GH";

    This is untested, so I have no idea if it will work or not. The switch may not actually be the best solution, since it sounds like you'll need to do extra testing.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    The loop itself is a problem since you're not incrementing it anywhere, so it will infinitely loop.
    My teacher asked us to use loops to do it, so how would I incriminate it? Should I add a count to the loop and limit the number of times it loops?

    My whole problem is I can't wrap my mind around how to correctly do this with a loop. All other approaches I can find seem to do it with topics I haven't covered yet and therefore can't use in my assingment.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    In a for loop you need to control it somehow. Generally, you'd see something like for(int i = 0; i < count; ++i), where the final parameter is the post evaluation condition (the increment). You could ignore that and increment i within the loop itself, but you need to do something to control it or it will infinitely loop. You could potentially use infinite loops for sockets and stuff where you need to listen indefinitely. Those would be controlled with:
    Code:
    for (;;)
    {
        // Do stuff.  If error, cause 'break'
    }
    // or
    while(true)
    {
        // do stuff.  If error, cause 'break'
    }
    I can see the loop with the string tokenizer, but the only other potential loop I can see is if a condition is unsatisfied.
    Code:
    boolean isSatisfied = false;
    while (!isSatisfied)
    {
        // Get the last character, evaluate it.  If not satisfied, check the previous character,
        // and so forth until satisfied is true.
    }
    So if you need to check up to the last three characters, you can do that with a loop:
    Code:
    boolean isSatisfied = false;
    int iWordLength = word.length();
    for (int i = iWordLength - 1; (i > iWordLength - 3) && !isSatisfied; --i)
    {
        // Now, we start at the last character.  We will need to branch out from this point
        // Once the determination is made, set isSatisfied to true
    }
    There are several approaches you can take. The loop itself has little weight compared to the conditional checks you'll need to make to make a determination.

    Aside from that, I also cannot see how you're expected to use loops on this.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well the loop was involved with the string tokenizer part, but I just figured that part out, I forgot to define a method earlier in the code.

    Anyway I was being ignorant before, I need to use if statements. There doesn't need to be a loop, I just need to use if statements. I may have just figured this out, but an I'd love to see an example someone using nested if statements to do this.


  •  

    Posting Permissions

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