How to delete the last character in a string?
Lets say I have a string "/data/share/" I want to delete the last character from the string ie "/" so that output will be "/data/share"; Here are some methods to do this in C language.
one way could be use of strncat() function;
then adds a terminating `\0'.
Another simple way is,
say the string variable is str_p
Then simply put
str_p[(strlen(str_p)-1)] = '\0';
And you are done;
Lets say I have a string "/data/share/" I want to delete the last character from the string ie "/" so that output will be "/data/share"; Here are some methods to do this in C language.
one way could be use of strncat() function;
char * strncat(char *restrict s1, const char *restrict s2, size_t n);The strncat() function appends not more than n characters from s2, and
then adds a terminating `\0'.
#include
Another simple way is,
say the string variable is str_p
Then simply put
str_p[(strlen(str_p)-1)] = '\0';
And you are done;
Comments