PDA

View Full Version : How do you print duplicate characters from a string?



gunjanjain
07-03-2019, 02:37 AM
Hello friends,

How do you print duplicate characters from a string?

Neo_5678
07-03-2019, 02:57 AM
Do you really think that this is the right place to post a thread related to coding?

This is the SEO category black magic baba! So don't spam here.!

eCommerceChamp
07-03-2019, 03:05 AM
// C++ program to count all duplicates
// from string using hashing
# include <iostream>
using namespace std;
# define NO_OF_CHARS 256

class gfg
{
public :

/* Fills count array with
frequency of characters */
void fillCharCounts(char *str, int *count)
{
int i;
for (i = 0; *(str + i); i++)
count[*(str + i)]++;
}

/* Print duplicates present
in the passed string */
void printDups(char *str)
{
// Create an array of size 256 and fill
// count of every character in it
int *count = (int *)calloc(NO_OF_CHARS, sizeof(int));
fillCharCounts(str, count);

// Print characters having count more than 0
int i;
for (i = 0; i < NO_OF_CHARS; i++)
if(count[i] > 1)
printf("%c, count = %d \n", i, count[i]);

free(count);
}
};

/* Driver code*/
int main()
{
gfg g ;
char str[] = "test string";
g.printDups(str);
//getchar();
return 0;
}

// This code is contributed by SoM15242