forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_duplicate_string.cpp
47 lines (42 loc) · 1.03 KB
/
print_duplicate_string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// C++ program to count all duplicates from string using hashing
//
// The All ▲lgorithms Project
//
// https://allalgorithms.com/strings
// https://github.com/allalgorithms/cpp
//
// Contributed by: Tushar Kanakagiri
// Github: @tusharkanakagiri
//
#include <stdio.h>
#include <stdlib.h>
#define NO_OF_CHARS 256
/* 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 program to test to pront printDups*/
int main()
{
char str[] = ""; //Enter string here
printDups(str);
getchar();
return 0;
}