C编程:读取文件文本并尝试找出最长的单词时出现问题

问题描述 投票:2回答:1

我是编码的初学者,所以我可能在这里和那里都犯了很多菜鸟错误。我们在学校里完成了这项任务,目标是找出最长的单词,并将其与字符数一起打印出来。我已经走了很远,但是从现在开始,我很难找到问题所在。该程序大部分时间卡在迭代49、50、51和59上。我认为这是因为realloc对于longestWord变量返回NULL。

有什么想法可以解决这些问题?在此先感谢大家!

输入:

abc
abcde
abcdefghij
abcdefghij
abcdefghijklmnopq
abcdefghijklmnopq
abcdefghijklmnop
auf wiedersehen

预期输出:

17 characters in longest word: abcdefghijklmnopq

到目前为止,我的代码:

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

//_________//

FILE* fptr;
int c;
int iteration=0;     //Just to keep track 



//___________Main____________//

int main()
{

    fptr = fopen("C:\\....\\input", "r");

    char *s;
    char *longestWord;
    int i=1, charCount=0;

    s = (char*) malloc (sizeof(char));
    longestWord = (char*) malloc (sizeof(char));

    while((c=fgetc(fptr)) != EOF){
        iteration++;
        printf("Iteration %d\n",iteration);
        if (isalpha(c)!=0 ){

            s=realloc(s,i*sizeof(char));
            s[i]=c;
            i++;
        }

        else if(c==' ' || c =='\n'){
            if(i>charCount){
                charCount=i-1;
                longestWord=realloc(longestWord,i*sizeof(char));


                while(longestWord == NULL){
                         longestWord=realloc(longestWord,i*sizeof(char));
                }
                for(int t=0;t<i;t++){
                        *(longestWord+t)=*(s+t);

                }

                i=1;

            }

            else{
               printf("*********Checkpoint 3***************\n");                //Checkpoint 3
               i=1;

            }

        }

        else{
            printf("\n\n********Error, got to the else section of the program********\n\n");
        }

    }

    printf("%d characters in the longest word: %s\n",charCount, longestWord);

    free(s);
    free(longestWord);
    fclose(fptr);
    return 0;

} //_____________END OF MAIN____________ //

c sorting realloc file-read isalpha
1个回答
1
投票

这里是您所执行代码的更新版本。

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

int main()
{
    FILE* fptr;
    int c;
    char *s;
    char *longestWord;
    int i=0;

    fptr = fopen("input.txt", "r"); // TODO: validate if fptr is not NULL

    // TODO: validate the return of mallocs
    s = (char*) malloc (sizeof(char)); // allocates 1 element
    longestWord = (char*) malloc(sizeof(char));

    while((c=fgetc(fptr)) != EOF){      
        if (isalpha(c) != 0 ){
            s=realloc(s, strlen(s)+1);
            s[i]=c;
            i++;
        }
        else if(c==' ' || c =='\n'){            
            s[i] = '\0';

            // check if it is the longest
            if(strlen(s) > strlen(longestWord)) {
                longestWord = realloc(longestWord, strlen(s)+1);
                strcpy(longestWord, s);
            }

            memset(s, '\0', strlen(s)+1);
            i=0;
        }
        else{
            printf("Weird character %c\n", c);
        }
    }

    printf("%ld characters in the longest word: %s\n", strlen(longestWord), longestWord);

    free(s);
    free(longestWord);
    fclose(fptr);
    return 0;
}

请注意以下内容:

  • 缺少fopenmalloc,...等函数的返回值的验证;
  • 全局变量在此特定程序中没有意义,因此已移至主要功能内;
  • 带有不属于[a-zA-Z]的字符的单词将被忽略。
© www.soinside.com 2019 - 2024. All rights reserved.