c - 找到行中的回文并显示它们

标签 c visual-studio palindrome

提供由不超过 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. 回文从头向前读或从尾向后读时相同的一个或一组单词。

关于c - 找到行中的回文并显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53482322/

相关文章:

c - 如何在 c 中结束重定向的 execlp

c# - 来自 C# : "DLL initialization routine failed" on Windows 10 的 gcc DLL

java - 单词数组中的回文对

visual-studio - Visual Studio 中的 "Publish"如何工作?

java - 循环和堆栈,回文家庭作业

python - 删除 Python 中的空格不起作用

c - 使用 printf 显示宽字符

c++ - FFmpeg -- 使用硬件加速进行视频解码

c - 在 malloc 中进行类型转换

c# - 无法在 C# 中的按钮文本中显示 "&&"