String Program

Input a string from the user and print the number of iterations it will take to make the string unique for each iteration we need to remove adjacent common characters from the string. (March,2019)
Sample Input
aabccda
Sample Output
2
Solution:
#include<stdio.h>
#include<string.h>
int main() {
    int i,j,no_of_iters=0;
    char str[50];
    fgets(str,50,stdin);    //gets(str)
    for(i=1;i<strlen(str)-1;i++){
        if(str[i-1]==str[i] || str[i+1]==str[i]){
            for(j=i;j<strlen(str);j++)
                str[j]=str[j+1];
            no_of_iters++;
            i--;
        }
    }
    printf("%d",no_of_iters);

}
Test Code


No comments:

Post a Comment

Total Pageviews