c - 在文件中查找单词,检查它们是否是回文

标签 c palindrome

我试图编写一个程序,从读取文件中查找一个或多个单词,并检查它是否是回文(两侧相同的单词),如果是,则将它们保存到另一个由回车符分隔的文件中。阅读文件中的单词可以以任何方式写入:用空格分隔、在句子中或用回车符分隔。

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAX 255

int palindrome(char *x, int y, int i) 
{
    while(i<=y){
        if(x[i]!=x[y])
            return 0;
        i++;y--;
    }
    return 1;
}

int main()
{
    char *reading; 
    int length;
    int x=0;
    int y=0;
    char read[MAX];
    char write[MAX];
    FILE *r;
    FILE *w;
    puts("Enter read file name");
    scanf("%s", read);
    puts("Enter write file name");
    scanf("%s", write);
    r=fopen(read, "r"); 
    if(r==NULL)
        perror("File does not exist");
    w=fopen(write, "w");
    reading=malloc(MAX*sizeof(char));
    while(fgets(reading, MAX, r)!=NULL) 
    {
        length=strlen(reading);
        while(x<=length){
            for(x=y; ;x++){
                printf("%c\n", reading[x]);
                if((reading[x]>='a'&& reading[x]<='z') || (reading[x]>='A' && reading[x]<='Z'))
                    break;
            }
            for(y=x; ;y++){
                printf("%c\n",reading[y]);
                if((reading[y]>='a'&& reading[y]<='z') || (reading[y]>='A' && reading[y]<='Z'));
                else
                    break;
            }
            if(palindrome(reading, y, x)==1)
                for( ;x<=y;x++)
                fputc(reading[x], w);
            x=y;
        }
    }
    fclose(r);
    fclose(w);
    return 0;
}

问题是代码不起作用,如何修复它?

最佳答案

我在您的代码中发现了一个错误,其中 x 的限制错误

length=strlen(reading);
while(x<=length){

所以我对解决问题的方式做了相当多的改变,而不是弄清楚你可能会打破哪些其他限制。我已将输出写入 stdout 而不是文件。请注意,我还在文件打开模式中添加了 "t"

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 255

int palindrome(char *string, int x, int y) 
{
    if (x >= y)
        return 0;
    while (y > x)
        if (tolower(string[x++]) != tolower(string[--y]))
            return 0;
    return 1;
}

int main()
{
    char reading[MAX+1]; 
    char readfile[MAX+1]; 
    int x, y, i;
    FILE *r;
    puts("Enter read file name");
    scanf("%s", readfile);
    r=fopen(readfile, "rt"); 
    if(r==NULL)
        perror("File does not exist");
    else {
        while (fgets (reading, MAX, r) != NULL) {
            x = 0;
            do {
                while (reading[x] && !isalpha (reading[x]))
                    x++;
                y = x;
                while (isalpha (reading[y]))
                    y++;
                if (palindrome (reading, x, y)) {
                    printf ("Palindrome: ");
                    for (i=x; i<y; i++)
                        printf ("%c", reading[i]);
                    printf ("\n");
                }
                x = y;
            }
            while (reading[x]);
        }
        fclose(r);
    }
    return 0;
}

关于c - 在文件中查找单词,检查它们是否是回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27088110/

相关文章:

c - 我可以使用什么来代替 scanf 进行输入转换?

java - 限制这个回文码的处理时间

java - 调试回文数代码中的 for 循环

c - 汇编中有关 win32 api 的帮助

c - 由两个 3 位数字的乘积组成的最大回文数。使用 c 。我的代码有什么问题吗?

c - 找到给定回文日期的最近回文日期

java - 回文排列(破解编码面试 1.4)

c - 使用 %d 说明符在 C 中打印 unsigned char 时出现奇怪的结果

c# - Environment.SetEnvironmentVariable 未设置任何值

c - UVa 102 - Ecology Bin Packing 错误答案