My Tech notes: Delete Last Char in String C program Simple Way
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Wednesday, October 29, 2008

Delete Last Char in String C program Simple Way

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;
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;

No comments:

Post a Comment