How to remove first occurrence of a character from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
 
void remove_first_occurrences(char s[], char ch) {
    int i  = 0, len = strlen(s);
    while(i < len && s[i] != ch)
        i++;

    while(i < len) {
        s[i] = s[i + 1];
        i++;
    }
}
 
int main() {
    char s[] = "c c++ c# java php python cobol";

    remove_first_occurrences(s, 'p');
	
	puts(s);
     
    return 0;
}
  
  
       
/*
run:
    
c c++ c# java hp python cobol
 
*/

 



answered Jul 5, 2020 by avibootz
...