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;
}

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.