Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,104 questions

40,777 answers

573 users

How to remove all occurrences of a character from string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
  
void remove_all_occurrences(char s[], char ch) {
	int len = strlen(s);
	
    for (int i = 0; i < len; i++) {
        if (s[i] == ch || s[0] == ' ') {
            for (int j = i; j < len; j++) {
                s[j] = s[j + 1];
            }
            len--;
            i--;
        }
    }
}
  
int main() {
    char s[] = "c c++ c# java php python cobol";
 
    remove_all_occurrences(s, 'p');
    puts(s);
	
	remove_all_occurrences(s, 'x');
    puts(s);
    
	remove_all_occurrences(s, 'c');
    puts(s);  
	  
    return 0;
}
   
   
   
        
/*
run:
     
c c++ c# java h ython cobol
c c++ c# java h ython cobol
++ # java h ython obol
  
*/

 





answered Jul 5, 2020 by avibootz
edited Jul 5, 2020 by avibootz

Related questions

1 answer 70 views
2 answers 89 views
1 answer 60 views
1 answer 84 views
...