-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_duplicates.c
46 lines (43 loc) · 1.03 KB
/
remove_duplicates.c
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
/* Remove 3 consecutive duplicates from string
For eg, Input: aabbbaccddddc
Output: ccdc */
#include <stdio.h>
#include <string.h>
void remove_dupe (char *s)
{
int curr=0, prev=0;
char *dstr, *sstr;
char p=' ';
while (*s) {
if (*s != p) {
prev = curr;
curr = 1;
} else {
curr++;
if (curr >= 3) {
// replace curr with the prev
curr = prev;
// shift left the remaining string
sstr = s+1;
dstr = s-2;
while (*sstr) {
*dstr = *sstr;
dstr++;
sstr++;
}
*dstr = '\0';
// move back curr ptr by the prev repeating count
s -= (prev+1);
}
}
p = *s;
s++;
}
}
int main() {
char a[] = "aabbccdddc";
char *b = strdup(a);
remove_dupe(a);
printf("String before %s, after %s\n", b, a);
return 0;
}