Home Strings Find the duplicates in a string.

Find the duplicates in a string.

by nikoo28
0 comment

Question: Find the duplicate characters in a string?

Input: floccinaucinihilipilification
Output: a ,c ,f ,i ,l ,n ,o

The idea for this problem would be:-

  • Create a character array for the number of characters possible. Take 256 for all the ASCII characters as well.
  • Traverse the array and print the characters that have a count greater than 1.

Here is the source code for the same-

#include <stdio.h>

// prints duplicates in str
void findDuplicates(char *str)
{
	
	int count[256] = {0};
	// Scan all characters one by one
    while (*str)
    {
        if (*str == ' ' || *str == '\n' || *str == '\t')
		{
			str++;
			continue;
		}
		
		count[*str]++;
		str++;
    }
 
    int i;
	for(i=0; i<256; i++)
	{
		if(count[i] > 1)
			printf("%c ,",i);
	}
}
 
// Driver program to test above function
int main(void)
{
    char str[] = "floccinaucinihilipilification";

	findDuplicates(str);

	return 0;
}
0 comment

You may also like

Enclose codes in [code lang="JAVA"] [/code] tags

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More