Valhalla Legends Forums Archive | C/C++ Programming | Replacing a character in a char array

AuthorMessageTime
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..
[code]
   for (i = 0; i <= strlen(text); i++)
   {
      if ((text == '"') || (text[i] == ','))
      {
          strcpy(text, text + i + 1);
      }
   }
[/code]

If I ran [i]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?
February 14, 2004, 12:48 AM
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:

[code]for ( i = 0, j = strlen( text ); i <= j; i++ )[/code]

See if that helps at all.
February 14, 2004, 7:45 AM
Adron
[code]
for (i = 0; i < strlen(text); i++)
{
if ((text[i] == '"') || (text[i] == ','))
{
memmove(text + i, text + i + 1, strlen(text+i+1));
i--;
}
}
[/code]

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.
February 14, 2004, 1:16 PM
Maddox
[quote author=Adron link=board=30;threadid=5282;start=0#msg44240 date=1076764611]
[code]
for (i = 0; i < strlen(text); i++)
{
if ((text[i] == '"') || (text[i] == ','))
{
memmove(text + i, text + i + 1, strlen(text+i+1));
i--;
}
}
[/code]

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.
[/quote]

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:
[code]
char *p = text;
while(*p)
{
if ((*p == '"') || (*p == ','))
memmove(p, p + 1, strlen(p + 1) + 1);
else
p++;
}
[/code]
February 14, 2004, 7:48 PM
Adron
[quote author=Maddox link=board=30;threadid=5282;start=0#msg44307 date=1076788135]
Additionally, this should work as well:
[code]
char *p = text;
while(*p)
{
if ((*p == '"') || (*p == ','))
memmove(p, p + 1, strlen(p + 1) + 1);
else
p++;
}
[/code]
[/quote]

As would this:
[code]
for(char *p = text, *q = text; *p == '"' || *p == ',' || (*q++ = *p); p++);
[/code]
February 14, 2004, 9:33 PM
UserLoser.
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
February 15, 2004, 1:41 AM

Search