How to delete N characters of a string by index in C

1 Answer

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

void del_char(char str[], int index, int total) {
   int j = 0;

   for (int i = 0; str[i] != '\0'; i++, j++) {
      if (i == (index - 1)) {
         i = i + total;
      }
      str[j] = str[i];
   }

   str[j] = '\0';
}

int main(){
   char s[] = "c programming";

   int N = 2;
   del_char(s, 3, N);

   puts(s);

   return 0;
}




/*
run:

c ogramming

*/

 



answered Sep 21, 2021 by avibootz
edited Sep 21, 2021 by avibootz

Related questions

1 answer 158 views
2 answers 182 views
2 answers 241 views
1 answer 179 views
1 answer 152 views
...