Thread: Removing characters from a string

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    15

    Removing characters from a string

    I'm learning strings and I have to write a program that (along with other things) takes a singular form of a noun and makes it plural. More specifically, the one I'm working with now is if the noun is baby, it would drop the "y" and add an "ies".

    The only part I have left to figure out is how to drop the "y" off the end of baby before using strcat to put "bab" and "ies" together to make it "babies".

    Any ideas/tips?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Replace the y with the end of string character '\0'. Also make sure that the destination string for your strcat() call is large enough to hold this new string.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    And how would you replace it with '\0'?
    This is what I have and it seems right. I just don't know how to replace the y with '\0'.

    Code:
    if (strcmp(&string1[stringlength-1], "y")==0) 
    	{
    		strcat(string1, "ies");
    		printf("%s", string1);
    	}

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    That business with the strcmp is needlessly complicated. You know a string is an array of characters, right? So why not simply
    Code:
    if( string1[stringlength-1]=='y' ) {
       replace string1[stringlength-1] with 'i';
       replace string1[stringlength] with 'e';
       replace string1[stringlength+1] with 's';
       replace string1[stringlength+2] with '\0';
    }
    ?.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    As long as string1 is large enough to hold the larger string.
    Code:
    if (string1[stringlength-1] == 'y') 
    	{
                    string1[stringlength-1] = '\0';
    		strcat(string1, "ies");
    		printf("%s", string1);
    	}
    If string1 is not large enough for the new larger string you will have problems. In this case string1 must be defined as char string1[8] at the very minimum.

    Jim

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laurenlb View Post
    I'm learning strings and I have to write a program that (along with other things) takes a singular form of a noun and makes it plural. More specifically, the one I'm working with now is if the noun is baby, it would drop the "y" and add an "ies".

    The only part I have left to figure out is how to drop the "y" off the end of baby before using strcat to put "bab" and "ies" together to make it "babies".

    Any ideas/tips?
    You don't need to remove the y first... just overwrite it with the new data.

    Code:
    char text[100];
    char *ptr;
    
    strcpy(text,"Baby");
    
    // find the y
    ptr = strchar('y');
    // change to plural
    if (ptr)
      strcpy(ptr,"ies");
    
    printf("%s", text);

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    strchr()
    But what if the word contains other 'y's.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    strchr()
    But what if the word contains other 'y's.
    Good point.

    I was concentrating on showing him how he didn't need to remove the last letter and overlooked that.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM