Removing last character in C

Removing last character in C

Just set the last char to be :

str[strlen(str)-1] = ;

In C, indicates a string ending.

Every string in C ends with . So you need do this:

int size = strlen(my_str); //Total size of string
my_str[size-1] = ;

This way, you remove the last char.

Removing last character in C

To be on the safe side:

if (str != NULL)
{
    const unsigned int length = strlen(str);
    if ((length > 0) && (str[length-1] == &)) str[length-1] = ;
}

Leave a Reply

Your email address will not be published.