Simple C question

This is a discussion on Simple C question within the C Programming forums, part of the General Programming Boards category; Hi - I'm new to this too - just learning.. my task is too pluralize nouns and this is what ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    1
    Hi - I'm new to this too - just learning.. my task is too pluralize nouns and this is what I have so far.... I can't seem to grasp the null part - figured out how to get the concat on the same line - but now have a zero in the middle... try church or boss... help? My class is C programming - using MS Visual C++ 5.0.

    #include <stdio.h>
    #include <string.h>


    int main(void)

    {
    char str[20], noun;

    printf("\nPlease enter a noun. The system will pluralize the noun for you. ");
    fgets(str,20,stdin);
    noun=strlen(str);

    if(str[noun-2]=='y')
    {
    str[noun-2]='\0';
    strcat(str,"ies");
    puts(str);
    }
    else if (str[noun-2]=='Y')
    {
    str[noun-2]='\0';
    strcat(str,"ies");
    puts(str);
    }
    else if (str[noun-2]=='s')
    {
    str[noun-1]='0';
    strcat(str,"es");
    puts(str);
    }
    else if (str[noun-2]=='S')
    {
    str[noun-1]='0';
    strcat(str,"es");
    puts(str);
    }
    else if ((str[noun-3]=='c')&&(str[noun-2]=='h'))
    {
    str[noun-1]='0';
    strcat(str,"es");
    puts(str);
    }
    else if ((str[noun-3]=='C')&&(str[noun-2]=='H'))
    {
    str[noun-1]='0';
    strcat(str,"es");
    puts(str);
    }
    else if ((str[noun-3]=='s')&&(str[noun-2]=='h'))
    {
    str[noun-1]='0';
    strcat(str,"es");
    puts(str);
    }
    else if ((str[noun-3]=='S')&&(str[noun-2]=='H'))
    {
    str[noun-1]='0';
    strcat(str,"es");
    puts(str);
    }
    else
    {
    str[noun-1]='0';
    strcat(str,"s");
    puts(str);
    }

    return(0);
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    ooo - *grimace* - bumping an old post by posting code without code tags....heh

    I say it takes less than 5 minutes to get this bad boy closed...

    edit::

    As an aside, insead of this:

    Code:
    str[noun - 1] = '0';
    try

    Code:
    str[noun - 1] = '\0';

    I am sure you can find the rest of the lines with the same problem...

    ~/
    Last edited by kermit; 10-05-2004 at 07:01 PM.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    It's much cleaner if you use toupper(). And switch() helps too I think:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void pluralize(char *str)
    {
      char *plural = NULL;
      int len = strlen(str);
    
      if(!*str)
        return;
      if(!(plural = malloc(len+3)))
      {
        puts("Memory allocation error");
        return;
      }
    
      switch(toupper(str[len-1]))
      {
        case 'Y':
          strncpy(plural, str, len-1);
          strcpy(plural+(len-1), "ies");
          break;
        case 'S':
          sprintf(plural, "%ses", str);
          break;
        case 'H':
          if(len > 1 &&
            (toupper(str[len-2]) == 'C' || toupper(str[len-2]) == 'S'))
          {
            sprintf(plural, "%ses", str);
            break;
          }
        default: // fall through from case 'H' if ending is not ch/sh
          sprintf(plural, "%ss", str);
      }
    
      puts(plural);
      free(plural);
    }
    
    int main(int argc, char **argv)
    {
      int i;
    
      if(argc < 2)
      {
        puts("Usage: plural <word>[ <word>[ ...]]");
        exit(EXIT_FAILURE);
      }
    
      for(i = 1;i < argc;++i)
        pluralize(argv[i]);
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Split from the bumped thread.

    @Dixie
    1. Read this - << !! Posting Code? Read this First !! >>
    2. Start new questions with new posts, not replies to old posts.
    3. Use the edit button at the bottom of your post to insert the code tags (see 1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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