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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

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

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
  
void remove_all_occurrences(char *s, const char ch) {
    int len = strlen(s);
 
    for (int i = 0; i < len; i++) {
        if (s[i] == ch) {
            for (int j = i; j < len; j++) {
                s[j] = s[j + 1];
            }
            len--;
            i--;
        }
    }
}


int main() {
    char s[] = "c c++ c# java php python rust";
  
    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 rust
c c++ c# java h ython rust
 ++ # java h ython rust
   
*/

 



answered Apr 1, 2019 by avibootz
edited Oct 12, 2024 by avibootz

Related questions

3 answers 202 views
1 answer 162 views
1 answer 169 views
1 answer 136 views
1 answer 150 views
...