I am trying to develop a logic that will remove all the adjacent duplicate characters from a string.
For example:-
Input: azxxzy
Output: ay
Here is the logic that I have developed in C:
int main()
{
char str1[10], str2[10];
int n, i=0, j=0,z=1, k=0;
scanf("%d", &n);
for(i=0; i<n; i++){
gets(str1);
str2[0]=str1[0];
for(j=1; str1[j]!='\0'; j++){
if(str1[j] == str1[j-1])
continue;
else
str2[z] = str1[j];
z++;
}
for(k=0; str2[k]!='\0'; k++)
printf("%s\n", str2[k]);
}
return 0;
}
While executing the code it is throwing a compile error. What might be the problem?
printf("%s\n",str2[k]);
str2[k] is a char, but you tell printf it is a char*
But this program still will not work properly - the first call to gets() will just read the carriage-return that is left in the input queue after reading the initial int value. And you never null-terminate str2.