-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintDuplicateCharacters.c
More file actions
51 lines (45 loc) · 1.07 KB
/
printDuplicateCharacters.c
File metadata and controls
51 lines (45 loc) · 1.07 KB
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
48
49
50
51
#include <stdio.h>
#include <string.h>
//Sorting the string using Insertion Sort
void InsertionSort(char str[], char nstr[]){
int len = strlen(str);
strcpy(nstr, str);
for(int k = 1; k < len; k++){
int j = k - 1;
char key = nstr[k];
while(j >= 0 && key < nstr[j]){
nstr[j + 1] = nstr[j];
j--;
}
nstr[j + 1] = key;
}
nstr[len] = '\0';
}
//Finding the duplicate characters
void duplicateChar(char* str){
char m = '\0';
int len = strlen(str);
for(int k = 0; k < len - 1; k++){
if(str[k] == m){
continue;
}
else if(str[k] == str[k + 1]){
printf("%c ", str[k]);
m = str[k];
}
}
}
int main(){
//Taking String input
char str[255];
char nstr[255];
printf("Input a string: ");
scanf("%s", str);
//Sorting the array
InsertionSort(str, nstr);
//Finding the duplicates and printing them.
printf("\nThe duplicate characters are: ");
duplicateChar(nstr);
putchar('\n');
return 0;
}