C编程:读取文件文本和尝试排序最长单词时出现问题

C编程:读取文件文本和尝试排序最长单词时出现问题,c,sorting,realloc,file-read,isalpha,C,Sorting,Realloc,File Read,Isalpha,我是一个编程新手,所以我可能会犯很多新手的错误。我们在学校里完成了这项任务,目标是把最长的单词和它的字符数一起打印出来。我已经走了这么远,但从现在开始,我很难找到问题所在。大多数情况下,程序都会在迭代49、50、51和59中卡住。我认为这是因为realloc为longestWord变量返回NULL 有没有解决这些问题的方法?提前谢谢各位 输入: abc abcde abcdefghij abcdefghij abcdefghijklmnopq abcdefghijklmnopq abcdefgh

我是一个编程新手,所以我可能会犯很多新手的错误。我们在学校里完成了这项任务,目标是把最长的单词和它的字符数一起打印出来。我已经走了这么远,但从现在开始,我很难找到问题所在。大多数情况下,程序都会在迭代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____________ //

#包括
#包括
//_________//
文件*fptr;
INTC;
int迭代=0//只是为了跟踪
//___________主要____________//
int main()
{
fptr=fopen(“C:\\..\\input”,“r”);
char*s;
字符*最长单词;
int i=1,charCount=0;
s=(char*)malloc(sizeof(char));
longestWord=(char*)malloc(sizeof(char));
而((c=fgetc(fptr))!=EOF){
迭代++;
printf(“迭代%d\n”,迭代);
如果(isalpha(c)!=0){
s=realloc(s,i*sizeof(char));
s[i]=c;
i++;
}
else如果(c=''| | c=''\n'){
如果(i>charCount){
charCount=i-1;
longestWord=realloc(longestWord,i*sizeof(char));
while(longestWord==NULL){
longestWord=realloc(longestWord,i*sizeof(char));
}

对于(int t=0;t,这里是代码的更新版本,它满足您的要求

#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;
}
#包括
#包括
#包括
#包括
int main()
{
文件*fptr;
INTC;
char*s;
字符*最长单词;
int i=0;
fptr=fopen(“input.txt”,“r”);//TODO:验证fptr是否为NULL
//TODO:验证mallocs的返回
s=(char*)malloc(sizeof(char));//分配1个元素
longestWord=(char*)malloc(sizeof(char));
而((c=fgetc(fptr))!=EOF){
如果(isalpha(c)!=0){
s=realloc(s,strlen(s)+1;
s[i]=c;
i++;
}
else如果(c=''| | c=''\n'){
s[i]='\0';
//检查它是否是最长的
if(strlen(s)>strlen(longestWord)){
longestWord=realloc(longestWord,strlen+1);
strcpy(最长单词,s);
}
memset(s),\0',strlen(s)+1;
i=0;
}
否则{
printf(“奇怪的字符%c\n”,c);
}
}
printf(“%ld个字符在最长的单词中:%s\n”,strlen(longestWord),longestWord);
免费的;
免费(最长单词);
fclose(fptr);
返回0;
}
请注意以下事项:

  • 缺少对函数返回值的验证,例如
    fopen
    malloc
  • 全局变量在这个特定程序中没有意义,因此被移动到主函数中
  • 具有不属于[a-zA-Z]的字符的单词将被忽略

通过使用s=realloc(s,(i+1)*sizeof(char))解决了问题;