Advertisement

How do I remove elements in an array?

Started by May 24, 2001 09:24 AM
10 comments, last by Zeke 23 years, 8 months ago
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
Advertisement
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.

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.


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.
you could also use an array of pointers. each element would point to a string, and you can just use whatever element you want
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;
Advertisement
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.
Here's another way to do it.

    #include <string.h>main(){   char Array[22]={"Hello world I am fine"};   char temp[22];   int i, j=0;   // Copy the original string to temp;   strcpy(temp,Array);   // Now move only the characters that you want back in order   for(i=0; i<6; i++) Array[j++]=temp[i];   for(i=12; i<21; i++) Array[j++]=temp[i];   // Make sure to end the Array string correctly   Array[j]='\0';   // And we're done.  Yay!   printf("%s\n",Array);}    


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.

if you wanted to generalise it...

          // note... sytnax might not be quite right, this was written off the top of my head.void DeleteWordFromString(char* str, const char* word){  char* ptr = 0;  wlen = strlen(word); // may need to subtract 1...  ptr = strstr(str,word);  if(ptr)  {    // yipee, we found the word    strcpy(ptr,ptr+wlen);  }}          

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