How to remove the middle character from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
 
void remove_middle_character_from_string(char s[]) {
    int size = strlen(s);
    int mid = size / 2;
     
    char tmp[256] = "";
     
    strncpy(tmp, s, mid);
    strncpy(tmp + mid, s + mid + 1, size - mid - 1);
     
    tmp[size - 1] = '\0';
     
    strcpy(s, tmp);
}
 
int main() {
    char s1[] = "abcde";
    remove_middle_character_from_string(s1);
    printf("%s\n", s1);
     
    char s2[] = "abcdef";
    remove_middle_character_from_string(s2);
    printf("%s\n", s2);
     
    return 0;
}
  
  
  
  
/*
run:
  
abde
abcef
  
*/

 



answered Mar 15, 2024 by avibootz
edited Jun 12, 2024 by avibootz

Related questions

2 answers 175 views
2 answers 144 views
1 answer 108 views
1 answer 97 views
1 answer 116 views
1 answer 149 views
...