to eliminate the two same chars adjacent to each other recursively

.comment { color: #999999; font-style: italic; } .pre { color: #000099; } .string { color: #009900; } .char { color: #009900; } .float { color: #996600; } .int { color: #999900; } .bool { color: #000000; font-weight: bold; } .type { color: #FF6633; } .flow { color: #FF0000; } .keyword { color: #990000; } .operator { color: #663300; font-weight: bold; } .operator { color: #663300; font-weight: bold; }

#include <stdio.h>

void
removeSpace(char *s) {

char
*p = s;

char
c;

while
((c = *s++)) {
if
(c != '\n' && c != '\t' && c != ' ')
*
p++ = c;
}

*
p = '';

}


void
removeCouple(char *str) {

char
*p = str;

char
*q;

while
(*p) {

//find couple

while(*p && *p != *(p + 1)) p++;

if
(!*p) break;

q = p - 1;

//replace couple with space
*p++ = ' ';
*
p++ = ' ';

while
(q >= str && *p) { //go backward

if
(*q == ' ') {
--
q; continue;

}
else if(*q == *p) { //now q && p are couple, replace them with space

*q-- = *p++ = ' ';

}
else
break
;
}

}


//finally remove space
removeSpace(str);

}


int
main() {
char
str[]= "BBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRGBBGBGRRBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGB";

printf("%s\n", str);

removeCouple(str);


printf("\n\n%s\n", str);
}
Advertisements

One thought on “to eliminate the two same chars adjacent to each other recursively

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s