[SOLVED] How to remove first character from C-string?

Issue

Can anyone please help me? I need to remove the first character from a char * in C.

For example, char * contents contains a '\n' character as the first character in the array. I need to detect and eliminate this character, modifying the original variable after its been “sanitized”.

Can anyone help me with the code? I’m completely new to C, and just can’t seem to figure it out.

Solution

if (contents[0] == '\n') 
    memmove(contents, contents+1, strlen(contents));

Or, if the pointer can be modified:

if (contents[0] == '\n') contents++;

Answered By – ruslik

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *