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 get the repeating characters of a string in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
      
char *get_repeating_chars(char* s) { 
	int len = strlen(s), k = 0;;
	char *tmp = (char *)malloc((len * sizeof(char)) + 1);
	
    for (int i = 0; i < len; i++) { 
        for (int j = i + 1; j < len; j++) { 
            if (s[i] == s[j]) { 
				if (!strchr(tmp, s[i])) {
					tmp[k++] = s[i]; 
					break;
				}
			}
		} 
	} 
	tmp[k] = '\0';

    return tmp; 
} 
  
int main() 
{ 
    char s[] = "abcdeffghijgklmbbbbxzx"; 
    char *tmp = get_repeating_chars(s); 

	puts(tmp);
	free(tmp);

    return 0; 
} 
 
        
        
        
/*
run:
        
bfgx
        
*/

 



answered Jan 9, 2020 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void get_repeating_chars(char* s, char *tmp) { 
	int len = strlen(s), k = 0;;
	
    for (int i = 0; i < len; i++) { 
        for (int j = i + 1; j < len; j++) { 
            if (s[i] == s[j]) { 
				if (!strchr(tmp, s[i])) {
					tmp[k++] = s[i]; 
					break;
				}
			}
		} 
	} 
	tmp[k] = '\0';
}
  
int main() 
{ 
    char s[] = "abcdeffghijgklmbbbbxzx";
	char *tmp = (char *)malloc((strlen(s) * sizeof(char)) + 1);
    
	get_repeating_chars(s, tmp); 
	puts(tmp);
	free(tmp);
	
    return 0; 
} 
 
        
        
        
/*
run:
        
bfgx
        
*/

 



answered Jan 9, 2020 by avibootz

Related questions

1 answer 107 views
1 answer 115 views
1 answer 179 views
1 answer 120 views
1 answer 134 views
1 answer 158 views
...