How to delete character from a string by index in C

2 Answers

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";

   del_char(s, 3, 1);

   puts(s);

   return 0;
}




/*
run:

c rogramming

*/

 



answered Sep 21, 2021 by avibootz
edited Sep 21, 2021 by avibootz
0 votes
#include <stdio.h>
#include <string.h>


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

    puts(s);

    int index = 2;
    memmove(&s[index], &s[index + 1], strlen(s) - index);

    puts(s);

    return 0;
}




/*
run:

c programming
c rogramming

*/

 



answered Sep 21, 2021 by avibootz

Related questions

2 answers 216 views
1 answer 125 views
1 answer 132 views
2 answers 240 views
...