-6

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?

4

1 回答 1

2
printf("%s\n",str2[k]);

str2[k] 是一个字符,但你告诉 printf 它是一个字符*

但是这个程序仍然不能正常工作 - 第一次调用 gets() 将只读取在读取初始 int 值后留在输入队列中的回车。而且您永远不会以空值终止 str2。

于 2016-04-04T22:15:41.037 回答