Help With Strcat

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
deus_the_programmer

Help With Strcat

Post 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.
Gnome

RE:Help With Strcat

Post 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
EyeScream

RE:Help With Strcat

Post by EyeScream »

if (len = strlen(str))
  str[len - 1] = '\0';

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

RE:Help With Strcat

Post 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
Post Reply