How to remove a pair of same adjacent characters from string in C

1 Answer

0 votes
#include <stdio.h>      
#include <string.h>   

void removeAdjacentPair(char *s) {
    int len = strlen(s);
    int j = 0;
    for (int i = 1; i < len; i++) {
        while ((s[i] == s[j]) && (j >= 0)) {
            i++;
            j--;
        }
        s[++j] = s[i];
    }
    s[j + 1] = '\0'; 
}

int main(void) {
	char s[] = "aabcccdeeffffgac"; 

	removeAdjacentPair(s);

	puts(s);

	return 0;
}
  
  
  
/*
run:
  
bcdgac
  
*/

 



answered Jun 19, 2020 by avibootz
...