c-在行中查找回文并显示它们



将提供一个由不超过100个字符的单词组成的字符串。单词由拉丁字符组成,并由一个空格分隔。有必要向标准输出流输出一个仅包含单词回文的字符串。源数据必须作为一个整体读取到内存中,所有操作都必须在内存中执行,结果在内存中获得,然后打印。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check(char str[])
{
int i, length;
length = strlen(str);
for (i = 0; i < length; i++)
if (str[i] != str[(length - 1) - i]) return 0;
return 1;
}
int main(void)
{
char str[100];
char* t;
gets(str);
t = strtok(str, " ");

while (t != NULL) {
if (check(t) == 1) {
printf("%s ", t);
}
t = strtok(NULL, " ");
}
_getch();
return 0;
}

这是我的代码(它未通过测试(请帮我修复代码

  1. 例如,使用函数getline(&buffer, &size, stdion)代替fgets
  2. check函数中的for循环运行良好,但解决了另一个问题,超出了预期
  3. 回文从开头向前或从结尾向后阅读时相同的单词或单词组

最新更新