Program to Make nouns plural

This is a discussion on Program to Make nouns plural within the C Programming forums, part of the General Programming Boards category; Hey guys.. I'm writing a program that converts singular nouns to their plural form. ie.. if the noun ends in ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Program to Make nouns plural

    Hey guys..

    I'm writing a program that converts singular nouns to their plural form.
    ie.. if the noun ends in "y" remove the "y" and add "ies"
    ends in "s", "ch", or "sh" add "es"
    and any other case add "s"

    So i have my program listed below. I'm trying to remove the last character of the string, analyze it, then determine what action needs to take place. Does anyone have any other opinions on how to tackle this problem or any suggestion on what i've written so far?

    Thanks,
    dtowndream
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char mystr[50] = {0}, tmp[50] = {0}, a[50]={0};
    int x;

    printf("Enter a string: ");
    gets(mystr);
    printf("Last char: %c\n",mystr[(strlen(mystr)-1)]);
    for(x=0;x<(strlen(mystr)-1);x++)
    tmp[x] = mystr[x];


    if((strlen(mystr)-1)= ' y ' ){
    printf("%sies\n",tmp);
    }
    else if(((strlen(mystr)-1)='s')){
    printf("%ses\n",mystr);
    }
    else if(((strlen(mystr)-1)='h')){
    printf("%ses", mystr);
    }
    else{
    printf("%ss", mystr);
    }
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could just save the last character, once. Then overwrite it with '\0', your string will then be one character shorter and you wont need the temp array, or keep calling strlen().

    Then you could compare your saved character with a switch/case statement.

    Also, consider using fgets, instead of gets. This lets you restrict the input to 50 chars to make sure that no more than 50 chars will be written to your array.
    Last edited by Subsonics; 11-09-2010 at 05:50 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Please use code tags (<< !! Posting Code? Read this First !! >>) when you post code in the future as well.

    You have a few substantial errors/misunderstandings to clear up:
    1. = is for assignment, == is for comparison
    2. strlen(mystr)-1 doesn't give you the last character, it simply gives you the index of the last character. The last character is mystr[strlen(mystr)-1], which you've determined already.

    Some general notes:
    What is the purpose of tmp and a? Do you ever use them or can you get rid of them?
    I suggest storing the length of mystr in a convenience variable, say "len". This will make your code more readable and more efficient (calling strlen is not free, it has to check every byte of the string every time).

    Also, I strongly suggest you try the following when you get around to testing your program. I think you will find a logical error your "ends in an h" check:
    bath->baths
    watch->watches
    bush->bushes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this program I'm trying to make
    By Sshakey6791 in forum C++ Programming
    Replies: 14
    Last Post: 12-01-2008, 03:03 PM
  2. Replies: 9
    Last Post: 06-17-2008, 11:38 AM
  3. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 01:05 AM
  4. A doubt on how to make a program
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 08-20-2004, 11:16 AM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 04:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21