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,926 questions

51,859 answers

573 users

How to remove all duplicate characters from a string in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>
    
void remove_duplicate_characters(char *s) {
  for (int i = 0; i < strlen(s); i++) {
        for (int j = i + 1; s[j] != '\0'; j++) {
            if (s[j] == s[i] && s[j] != ' ') {
                for (int k = j; s[k] != '\0'; k++) {
                    s[k] = s[k + 1];
                }
                
                j--;
            }
        }
    }
}
 
int main() {
    char s[64] = "ccc x ppppprooooogggramming x c dddd";
   
    remove_duplicate_characters(s);
       
    puts(s);
}
   
   
    
/*
run:
    
c x progamin   d
   
*/


 



answered Apr 1, 2019 by avibootz
edited Apr 10, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
    
void remove_duplicate_characters(char *s) {
    for (int i = 0; i < strlen(s); i++) {
        for (int j = i + 1; s[j] != '\0'; j++) {
            if (s[j] == s[i] && s[j] != ' ') {
                strcpy(s + j, s + j + 1);
                j--;
            }
        }
    }
}
 
int main() {
    char s[64] = "ccc x ppppprooooogggramming x c dddd";
   
    remove_duplicate_characters(s);
       
    puts(s);
}
   
   
    
/*
run:
    
c x progamin   d
   
*/


 



answered Apr 3, 2019 by avibootz
edited Apr 10, 2024 by avibootz

Related questions

1 answer 116 views
1 answer 209 views
3 answers 183 views
3 answers 148 views
2 answers 124 views
2 answers 186 views
2 answers 174 views
...