How do I remove elements in an array?
Say i have a char array char Array[21]={"Hello world I am fine"} or something, how can i remove for example "world" so I end up with {"Hello I am fine"}.
I can replace the elements with 0 so i end up with {"Hello I am fine"} but I dont want the big space.
Thanks for any help.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
strcpy( &Array[6], &Array[12] ) would do it...
Btw, that string is 21 characters long, so you''ll need to make the array 22 characters: char Array[22] ...
Also Btw, you can''t just replace elements with 0, since 0 is the Null character, which signifies the end of a string...
G''luck,
-Alamar
Btw, that string is 21 characters long, so you''ll need to make the array 22 characters: char Array[22] ...
Also Btw, you can''t just replace elements with 0, since 0 is the Null character, which signifies the end of a string...
G''luck,
-Alamar
Thanks for the reply but i dont understand how that would work. It would copy Array[12] to Array[6] but how would it know to miss out "world" rather than another string?
Thanks for the tip on the array size, I always forget that.
Thanks for the tip on the array size, I always forget that.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Well there is honestly no way to do it easily. So let me just do a quick one.
This is what it should look like. I just did this off the top of my head so I''m not sure of how good it is. It''s basicaly to just give you the idea.
There is no way to just remove things, you could search through a string, and move the chars backwards also. I just prefer to copy it over to a new string.
char removeString[6] = { "World\n" };
char origionalString[22] = { "Hello World I am Fine\n" }
int searchInt = 0;
int origionalInt = 0;
int newInt = 0;
char *newString = new char[22-6];
// Go through the origional string and look for an end of line.
while(origionalString[origionalInt] != ''\n'')
{
// Check to see if we have matching letters.
if(origionalString[origionalInt] != removeString[searchInt])
{
// If not reset our search counter.
searchInt = 0;
// Copy the data over appropriately.
newString[newInt] = origionalString[origionalInt];
newInt++;
}
else
searchInt++;
origionalInt++;
}
This is what it should look like. I just did this off the top of my head so I''m not sure of how good it is. It''s basicaly to just give you the idea.
There is no way to just remove things, you could search through a string, and move the chars backwards also. I just prefer to copy it over to a new string.
Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
May 24, 2001 12:48 PM
you could also use an array of pointers. each element would point to a string, and you can just use whatever element you want
May 24, 2001 12:56 PM
an example of using an array of pointers:
char *word[5] = { "hello ", "world ", "i ", "am ", "fine." };
cout << word[0] << word[2] << word[3] << word[4] << endl;
char *word[5] = { "hello ", "world ", "i ", "am ", "fine." };
cout << word[0] << word[2] << word[3] << word[4] << endl;
May 24, 2001 08:00 PM
strcpy( &Array[6], &Array[12] )
should work just fine. Your response to his suggestion indicated that you did not try it. I haven''t either but I''m almost certain that it works perfectly. Give it a try, and then figure out why it works. Remember the number in the brackets is a pointer offset. You aren''t actually removing cells from the array, that is impossible. If it doesn''t work perfectly it is because of how strcpy is implemented, but the basic concept is right.
should work just fine. Your response to his suggestion indicated that you did not try it. I haven''t either but I''m almost certain that it works perfectly. Give it a try, and then figure out why it works. Remember the number in the brackets is a pointer offset. You aren''t actually removing cells from the array, that is impossible. If it doesn''t work perfectly it is because of how strcpy is implemented, but the basic concept is right.
Here's another way to do it.
Hope this helps.
Edited by - GameCreator on May 25, 2001 12:11:29 PM
|
Hope this helps.
Edited by - GameCreator on May 25, 2001 12:11:29 PM
or you could just use:
strcpy( &Array[6], &Array[12] );
which is one line as opposed to about 20.![](wink.gif)
if you wanted to generalise it...
then you can just remove any word you like from any string you like.
Edited by - Sandman on May 25, 2001 12:38:54 PM
strcpy( &Array[6], &Array[12] );
which is one line as opposed to about 20.
![](wink.gif)
if you wanted to generalise it...
|
then you can just remove any word you like from any string you like.
Edited by - Sandman on May 25, 2001 12:38:54 PM
quote:
strcpy( &Array[6], &Array[12] )
Be careful with this. I haven''t tried it, and I am not saying that it won''t work. but the documentation for strcpy states that the behavior of strcpy is undefined if the source and destination strings overlap.
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement