I can't slove this problem
my code print nothing
what's wrong with it ??

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define total 1000

int weight (char word[total])
{
    int i;
    int sum;
    for (i=0;word[i]!='\0';i++)
    {
        sum+=i;
    }

    return sum;
}

char *getword (char temp[total])
{
    static char max[total]={'\0'};
    int lenth,maxlenth;

    lenth=strlen(max);
    maxlenth=strlen(temp);

    if (lenth>=maxlenth)
    {
        if (lenth>maxlenth)
        {
            strcpy (max,temp);
        }

        if (lenth==maxlenth)
        {
            if(weight(temp)>weight(max))
            strcpy (max,temp);
        }
    } 

    return max;
}

int  main()
{
    char c,word[total];
    int i;
    int offset=0;
    int sum,sumword;

    while ((c=getchar())!=EOF)
    {    
        if (c==' ' ||c=='\t' ||c=='\n')
        {
            offset=0;
        }

        if (isalpha(c)) 
        {
            offset=1;
        }

        if(offset==1)
        {    
            sum ++;
            for(i=0;offset==0;i++)
            {
                word[i]=c;
                sumword ++;
            }

            strcpy(word,getword(word));
        }
    }               

    float ave=(float)sum/sumword ;

    printf("the longset word :%s\n",word);
    printf("Average word length:%.2f\n",ave);    

    return 0;
}

Dani AI

Generated

Summary of the real problems and a compact fix.

Several issues combine to make the program print nothing: character-reading uses char (EOF needs int); the inner loop that builds a word never runs because its condition is wrong; words are not null-terminated; some counters were uninitialized; getword compares lengths backwards; and weight() sums the loop index instead of character values. was right to recommend keeping the longest-word buffer in the caller (or otherwise manage lifetime), and was correct to call out uninitialized counters.

A simple, robust pattern for tokenizing and updating the longest-word (different from the posted attempts):

int c;
char word[total];
int i = 0, total_chars = 0, word_count = 0;
char maxword[total] = "";

while ((c = getchar()) != EOF) {
    if (isalpha((unsigned char)c)) {
        if (i < total - 1) word[i++] = c;
    } else if (i > 0) {
        word[i] = '\0';
        word_count++;
        total_chars += i;
        if (strlen(word) > strlen(maxword) ||
            (strlen(word) == strlen(maxword) && strcmp(word, maxword) > 0))
            strcpy(maxword, word);
        i = 0;
    }
}
/* handle trailing word if i>0 and check word_count!=0 before averaging */

Extra notes: always cast to (unsigned char) for isalpha; guard buffer writes with i < total-1; null-terminate every token; fix weight() if a custom tie-breaker is needed (or prefer strcmp for lexicographic tie-breaking); and check word_count before dividing to compute the average.

Recommended Answers

All 3 Replies

It looks to me that you're trying to return a pointer to a variable that is going out of scope as soon as the function ends. Try declaring the array in the calling function and returning void instead:

void getword (char temp[total], char max[total])

Also consider your uninitializaed variable sum on line 9. You should initialize it to something. I assume you want it initialized to 0. But right now you are incrementing an uninitialized variable on line 12, so you could get jibberish. Don't assume that the compiler will initialize it to 0. Even if YOUR compiler does, initialize it anyway because MY compiler may not.

commented: "Hey, it works here" - Everyday programmer. +13

thanks for your help
I try to code like this , but it still don't work.
where is my problem ??

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define total 1000

int weight (char word[total])
{
    int i;
    int sum=0;
    for (i=0;word[i]!='\0';i++)
    {
        sum+=i;
    }

    return sum;
}

void  getword (char temp[total],  char max[total])
{
    int lenth,maxlenth;

    lenth=strlen(max);
    maxlenth=strlen(temp);

    if (lenth>=maxlenth)
    {
        if (lenth>maxlenth)
        {
            strcpy (max,temp);
        }

        if (lenth==maxlenth)
        {
            if(weight(temp)>weight(max))
            strcpy (max,temp);
        }
    } 

}

int  main()
{
    char c,word[total],maxword[total]={'\0'};
    int i;
    int offset=0;
    int sum=0,sumword=0;

    while ((c=getchar())!=EOF)
    {    
        if (c==' ' ||c=='\t' ||c=='\n')
        {
            offset=0;
        }

        if (isalpha(c)) 
        {
            offset=1;
        }

        if(offset==1)
        {    
            sum ++;
            for(i=0;offset==0;i++)
            {
                word[i]=c;
                sumword ++;
            }

          getword(word,maxword);
        }
    }               

    float ave=(float)sum/sumword ;

    printf("the longset word :%s\n",maxword);
    printf("Average word length:%.2f\n",ave);    

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.