How to delete all characters except the last one from a string 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";
 
   del_char(s, 1, strlen(s) - 1);
 
   puts(s);
 
   return 0;
}
 
 
 
 
/*
run:
 
g
 
*/

 



answered Sep 22, 2021 by avibootz
...