#include <stdio.h>

int main (void)
{
int n;

printf("Give the number of words you want to input.");
scanf("%d",&n);

int letters[n],i,j,count,key,k;
char str[100];
 //Scans each word, counts it's letters and stores it in the next available 
 //position in "letters" array.
 for (i=0;i<n;i++)
    {
        j=0;
        printf("Give the next word.");
        do{
            str[j] = getchar();
            j++;
        }while (str[j-1]!='\n');
        str[j-1] = '\0';

        letters[i] = j;
    }

//Compacts the data by figuring out which cells have the same number of letters 
for (i=0;i<n;i++)
    {
        key = letters[i];
        count = 0;
        for (j=i+1;j<=n;j++)
            {
                if (key==letters[j])
                    {   
                        count += 1;
                        letters[j] = 0;
                    }
            }
        letters[i] = count;
    }

//creates a histogram
i=0;
do{
    printf("%d|",i);
    for (j=1;j<=letters[i];j++)
    {
        printf("*");
    }
    printf("\n");
    i++;
}while ((i<=n));

return 0;

}

I understand that getchar(); reads, the first enter (\n) , user hits, to give the amount of words he wants to input, and thus expects one less word.

Also, I get an infite loop for some reason at the end. Any help and ideas would be appreciated. Thanks in advance.

有帮助吗?

解决方案

Change the first block of your code to look like this:
(test the output of getchar, and continues only if not EOF)

for (i=0;i<n;i++)
{
    j=0;
    printf("Give the next word.");
    do{
        a = getchar();
        if(a >= 0) 
        {
            str[j] = a;
            j++;
        }
        else break;
    }while (str[j-1]!='\n');
    str[j-1] = '\0';

    letters[i] = j;
}

But regarding your question: How can I replace getchar();? Have you considered using scanf()?

EDIT
Here is a simple example of using scanf() and printf() to prompt for input and then display input. It will allow user to input entire words or sentences (up to 80 characters) until 'q' is entered. Not exactly what you are doing, but you should be able to adapt it to your code... (run this)

int main(void)
{
    char buf[80]={""};
    while(  strcmp(buf, "q") != 0)  //enter a 'q' to quit
    {
        buf[0]=0;
        printf("enter string:\n");
        scanf("%s", buf);
        printf("%s\n", buf);
    }

}

其他提示

Wouldn't it be easier to update the letter count in the first loop?

memset(letters, 0, n);
for (i=0;i<n;i++)
{
    char* s = str;
    int j=0;
    printf("Give the next word.");
    do{
        *s = getchar();
        ++j;
    }while (*(s++)!='\n');
    s[-1] = '\0';

    letters[j-1]++;
}

As a result the second loop will be unnecessary.

The following two lines have the wrong end condition; should be <n, not <=n. Currently they retrieve an uninitialized array element. Since you declared str as a local variable, that element is typically populated with garbage, i.e. a very big random number. That might explain why it takes extreme long (but possibly not forever) for the last loop to finish.

for (j=i+1;j<=n;j++)

}while ((i<=n));

Also, I assume line n of the histogram should contain the number of words that have n letters? That's not what you're doing right now.

letters[i] = count;

That line should have been:

letters[key] = count;

But to make that work, you should not overwrite the same array letters; you must declare a new array for your histogram, otherwise the second loop will destroy its own input.

By the way, str seems totally redundant. Is it there for debugging purposes?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top