Alex Rivera | Logout

Strip first and last character from C string

Asked 2009-11-13T00:05:48.233
28

I have a C string that looks like "Nmy stringP", where N and P can be any character. How can I edit it into "my string" in C?

Edit
Report

1 Answer

12

Another option, again assuming that "edit" means you want to modify in place:

void topntail(char *str) {
    size_t len = strlen(str);
    assert(len >= 2); // or whatever you want to do with short strings
    memmove(str, str+1, len-2);
    str[len-2] = 0;
}

This modifies the string in place, without generating a new address as pmg's solution does. Not that there's anything wrong with pmg's answer, but in some cases it's not what you want.

answered 2009-11-13T01:55:27.700

Your Answer