How to remove the first N characters from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <assert.h>

void removeFirstN(char* str, size_t N) {
    assert(N != 0 && str != 0);
    size_t len = strlen(str);
    if (N > len)
        return;  
    memmove(str, str + N, len - N + 1);
}

int main(void) {
    char str[] = "c programming";
    
    int N = 4;
    
    removeFirstN(str, N);

    puts(str);

    return 0;
}




/*
run:

ogramming

*/

 



answered Jun 13, 2022 by avibootz
...