C中的单词回文



我的任务是在文本文件中查找单词回文,而不是将它们打印到结果文件中。结果文件应仅包含所有非回文的空格和单词。我已经在这个程序上工作了两个星期,但由于我是 C 语言的新手,我无法简单地想象如何正确地做到这一点。另外,我必须在 Linux 环境中工作,所以我不能使用 strrev() 这样的命令,这会让我在这一点上的生活变得轻松得多......

无论如何,数据文件在很多行中包含很多单词,这些行由相当多的空格分隔。

这是正在运行的程序,但不适用于任何空格,因为我不知道如何在需要的地方检查它们。

#include <stdio.h> 
#include <string.h>
const int CMAX  = 1000;
const int Dydis = 256;
FILE *dataFile;
FILE *resFile;
void palindrome(char *linex);
int main(){
    char duom[CMAX], res[CMAX], linex[Dydis];
    printf("What's the name of data file? n");
    scanf("%s", duom);
    dataFile=fopen(duom, "r");
    if (dataFile==NULL){
        printf ("Error opening data file n");
        return 0;
    };
    printf("What's the name of results file? n");
    scanf ("%s", res);
    resFile=fopen(res, "w");
    if (resFile==NULL){
        printf ("Error opening results file n");
        return 0;
    };
    while (fgets(linex, sizeof(linex), dataFile)) {
        palindrome(linex);
    }
    printf ("all done!");
    fclose(dataFile);
    fclose(resFile);
}
void palindrome(char *linex){
    int i, wordlenght, j;
    j = 0;
    char *wordie;
    const char space[2] = " ";
    wordie = strtok(linex, space);
    while ( wordie != NULL ) {
        wordlenght = strlen(wordie);
        if (wordie[j] == wordie[wordlenght-1]) {
            for (i = 0; i < strlen(wordie); i++) {
                if (wordie[i] == wordie[wordlenght-1]) {
                    if (i == strlen(wordie)-1) {
                        fprintf(resFile,"");
                    }
                    wordlenght--;
                }
                else {
                    fprintf(resFile,"%s", wordie);
                    break;
                }
            }
        }
        else {
            fprintf(resFile,"%s", wordie);
        }
        wordie = strtok(NULL, space);
    }
}

编辑:

下面的代码如下:

  • 输入文件按字符读取
  • 如果字符读取不是字母数字,则将其写入输出文件
  • 否则,整个单词都fscanf
  • 如果 word 不是回文,则写入输出文件


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int is_pal(char* word) {
    size_t len = strlen(word);
    char* begin = word;
    char* end = word + len - 1;
    if (len == 1) {
        return 1;
    }
    while (begin <= end) {
        if (*begin != *end) {
            return 0;
        }
        begin++;
        end--;
    }
    return 1;
}
int main(void)
{
    FILE* fin = fopen("pals.txt", "r");
    if (fin == NULL) {
        perror("fopen");
        exit(1);
    }
    FILE* fout = fopen("out_pals.txt", "w");
    if (fout == NULL) {
        perror("fopen");
        exit(1);
    }
    int ret;
    char word[100];
    while ((ret = fgetc(fin)) != EOF) {
        if (!isalpha(ret)) {
            fprintf(fout, "%c", ret);
        }
        else {
            ungetc(ret, fin);
            fscanf(fin, "%s", word);
            if (!is_pal(word)) {
                fprintf(fout, "%s", word);
            }
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}

我创建了包含以下内容的文件:

cancer kajak anna sam truck
test1   abc   abdcgf  groove   void
xyz annabelle  ponton  belowoleb   thing
cooc  ringnir

输出文件:

cancer   sam truck
test1   abc   abdcgf  groove   void
xyz annabelle  ponton     thing
(line with two spaces)

如您所见,单词之间的空格数与输入文件中的空格数相同。

我假设单个单词最多可以有 100 个字符。如果有更长的单词,在固定大小的缓冲区上fscanf阅读可能是有害的。

提示

  • strtok()为您提供指向分隔词开头的指针,但它没有提取它们或将它们放入自己的字符串中。

  • 你需要一些逻辑来找到每个单词的结尾。函数 strlen()会告诉你字符中有多少个字符*直到空字符。如果你给它一个指向开始的指针句子中的单词,它将为您提供从开头的长度字到句尾。

  • palindrome()分解为一个函数,该函数循环访问一行中的单词和返回单个单词是否为回文的函数可能会有所帮助。

  • 您的 for 循环正在检查每对字母两次。 i只需要扫描一半以上的字长。

  • 您只需要palindrome()内的单个if。我不知道你为什么有这么多。它们是多余的。

最新更新