Favolas Favolas - 5 months ago 11
C Question

How to remove the character at a given index from a string in C?

How do I remove a character from a string?

If I have the string

and I want to remove
how do I do that?

Removing the first character is easy with this code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
char word[] = "abcdef";
char word2[10];


printf("%s\n", word2);

return 0;



will give me the string without the last character, but I still didn't figure out how to remove a char in the middle of a string.


Memmove can handle overlapping areas, I would try something like that (not tested, maybe +-1 issue)

char word[] = "abcdef";  
int idxToDel = 2; 
memmove(&word[idxToDel], &word[idxToDel + 1], strlen(word) - idxToDel);

Before: "abcdef"

After: "abdef"