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
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
Try this (I may have messed up with the ['s and ]'s, though):
I havn't tested it though, so it might need some 'tweaking'.Code:string greetings[] = {Hello, Wassup, Bye, Now, Laterz}; greetings[2] = greetings[3]; greetings[3] = greetings[4]; greetings[4] = "";
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.
>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]
i've never tried that before, i just made it up on the spot...i wonder if it works?
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]
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.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
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]