• Welcome to Valhalla Legends Archive.
 

Replacing a character in a char array

Started by UserLoser., February 13, 2004, 06:48 PM

Previous topic - Next topic

UserLoser.

I have a character array, and what I want to do is get rid of " and , in the array... I can't get it working correctly.  What I have, which doesn't work but I want something similar to it, but working..

   for (i = 0; i <= strlen(text); i++)
   {
      if ((text[i] == '"') || (text[i] == ','))
      {
          strcpy(text, text + i + 1);
      }
   }


If I ran test through that, it'd result in test - working
If I ran "test through that, it'd result in test - working
If I ran ,test through that, it'd result in test - working
If I ran test" through that, it'd result in test" - not working
If I ran ,test" through that, it'd result in test" - not working

So basically, if there were either a , or a " or both, on both sides of the character array, how can I remove those?

Zakath

Just offhand, I would say that you might have problems because you're changing the value against which your iterator is tested. When you call strcpy, you change the length of the string. Then it calls strlen, which potentially could return a new, smaller number. I would change the loop like this:

for ( i = 0, j = strlen( text ); i <= j; i++ )

See if that helps at all.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Adron


  for (i = 0; i < strlen(text); i++)
  {
     if ((text[i] == '"') || (text[i] == ','))
     {
        memmove(text + i, text + i + 1, strlen(text+i+1));
        i--;
     }
  }


Some small changes: Don't increase i after a match because you moved the text forward instead. Only move the text one position. The behaviour of strcpy is undefined when the strings overlap. No need to process the string terminator.

Maddox

#3
Quote from: Adron on February 14, 2004, 07:16 AM

  for (i = 0; i < strlen(text); i++)
  {
     if ((text[i] == '"') || (text[i] == ','))
     {
        memmove(text + i, text + i + 1, strlen(text+i+1));
        i--;
     }
  }


Some small changes: Don't increase i after a match because you moved the text forward instead. Only move the text one position. The behaviour of strcpy is undefined when the strings overlap. No need to process the string terminator.

This won't copy over the null terminator. You can correct this by changing strlen(text+i+1) to strlen(text+i+1)+1.

Additionally, this should work as well:

char *p = text;
while(*p)
{
     if ((*p == '"') || (*p == ','))
        memmove(p, p + 1, strlen(p + 1) + 1);
     else
        p++;
}
asdf.

Adron

Quote from: Maddox on February 14, 2004, 01:48 PM
Additionally, this should work as well:

char *p = text;
while(*p)
{
     if ((*p == '"') || (*p == ','))
        memmove(p, p + 1, strlen(p + 1) + 1);
     else
        p++;
}


As would this:

for(char *p = text, *q = text; *p == '"' || *p == ',' || (*q++ = *p); p++);
 

UserLoser.

#5
Thanks everyone!

I havn't tested these (been busy with other more important problems), but I'm assuming they work due to the fact of the people who replied :P