繁体   English   中英

在行中找到回文并显示它们

[英]find palindromes in the row and display them

提供的字符串由不超过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. 回文指的是从头开始向前或从头开始向后阅读时相同的一个单词或一组单词。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM