How do I delete a string w/in a string?

This is a discussion on How do I delete a string w/in a string? within the C Programming forums, part of the General Programming Boards category; hi, say I have an array of strings consisting of: Hello Wassup Bye Now Laterz and I want to remove ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    Question How do I delete a string w/in a string?

    hi,
    say I have an array of strings consisting of:
    Hello
    Wassup
    Bye
    Now
    Laterz

    and I want to remove the string Bye...how do I do that..I need to make sure that when I print it..all I get now is:
    Hello
    Wassup
    Now
    Laterz
    ???
    Help please
    A

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Try this (I may have messed up with the ['s and ]'s, though):
    Code:
    string greetings[] = {Hello, Wassup, Bye, Now, Laterz};
    
    greetings[2] = greetings[3];
    greetings[3] = greetings[4];
    greetings[4] = "";
    I havn't tested it though, so it might need some 'tweaking'.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Higly depends on how you constructed your "string of strings".
    Is it one string containing words with spaces between, or is it an array of pointers pointing to strings?

    Hmm, I have never used the string class as shown in Face_Masters example. Where can I get the lib?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Where can I get the lib?
    from a c++ compiler I expect!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    i've never tried that before, i just made it up on the spot...i wonder if it works?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here ya go:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define REMOVE_ITEM 2
    
    int main(void)
    {
    	char myarray[][20] = {"Hello", "Wassup", "Bye", "Now", "Laterz"};
    	int i, items_in_array = sizeof(myarray) / 20;
    	
    	for (i = 0; i < items_in_array; i++)
    		printf ("Array item %d is %s\n", i, myarray[i]);
    	
    	for (i = REMOVE_ITEM; i < items_in_array - 1; i++)
    		strcpy(myarray[i], myarray[i+1]);
    	
    	myarray[items_in_array-1][0] = '\0';
    	
    	printf ("After Delete:\n");
    	for (i = 0; i < items_in_array; i++)
    		printf ("Array item %d is %s\n", i, myarray[i]);
    		
    	return (0);
    	
    }
    
    /* Program Output:
    Array item 0 is Hello
    Array item 1 is Wassup
    Array item 2 is Bye
    Array item 3 is Now
    Array item 4 is Laterz
    After Delete:
    Array item 0 is Hello
    Array item 1 is Wassup
    Array item 2 is Now
    Array item 3 is Laterz
    Array item 4 is (null)
    */
    Last edited by Hammer; 05-16-2002 at 11:09 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Yep, I'd say that's better than mine

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    Umm..need more help

    But say..that I need to remove more names & don't know which names to remove...that depends on the functions preceding it..how would I modfiy the code to do so??
    I need it to work in a loop..and the e.g I gave was hypothetical..
    a

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Umm..need more help

    Originally posted by aspand
    But say..that I need to remove more names & don't know which names to remove...that depends on the functions preceding it..how would I modfiy the code to do so??
    I need it to work in a loop..and the e.g I gave was hypothetical..
    a
    Stick the for loop in a function, make the function take an array and an int as arguments.
    In another function (maybe), have another loop that starts at element 0 of the array and loops through it using strcmp() to find elements matching your criteria. If it finds one, it calls the earlier function telling it what element number to remove from the array. Clear as mud I know... but the ya go, it's been a long day
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM

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