Page 1 of 1

Help With Strcat

Posted: Wed Jun 02, 2004 11:00 pm
by deus_the_programmer
Hi i am using strcat to Append one string to another. it then puts the result into a buffer. I would like to be able to delete a character at the end of the buffer when the backspace key is pressed. ie press backspace, the cursor moves back one char and the last char in the buffer is removed from the buffer.

is there a function that does the opposite to strcat ie instead of adding a string to a string, one that deletes one char from a string?

please help
thanks,
ed.

RE:Help With Strcat

Posted: Wed Jun 02, 2004 11:00 pm
by Gnome
I'd just write over the last character with a null-terminator ('\0'). That effectively shortens the string by one character.

str[strlen(str)-2] = '\0';

Hope that helps

Gnome

RE:Help With Strcat

Posted: Thu Jun 03, 2004 11:00 pm
by EyeScream
if (len = strlen(str))
  str[len - 1] = '\0';

It should be "len - 1", not "len - 2"!

RE:Help With Strcat

Posted: Thu Jun 03, 2004 11:00 pm
by Gnome
You're right, my mistake. I was thinking that I had to account for an off-by-one with the length of the string (thus, I was thinking strlen(str) - 1 - 1, or strlen(str) - 2).

Thanks for noticing that error :)

Gnome