2

So I'm new in C language and I'm trying to do a histogram with the length of the words the user typed, I have a solution but my second for loop always fail, I code like 5 for loop and every of them just stop after the second or third iteration, Am I missing something please help. Here's my code.

#include<stdio.h>
int main(){
    int i,x,c,r,size;
    int wa[10];
    size=0;
    for(i=0;i<10;i++){
        wa[i]=0;
    }
    while((c=getchar())!=EOF){
        switch(c){
            case' ':{
                wa[size]++;
                size=0;
                break;}
            case'\n':{
                wa[size]++;
                size=0;
                break;}
            case'\t':{
                wa[size]++;
                size=0;
                break;}
            default:{
                size++;
                break;
            }
        }
    }

    for(r=0;r<=10;++r){
        printf("%d",r);
        for(x=0;x<wa[r];x++){
            printf("*");
        }
        printf("\n");
    }
    return 0;
}
7
  • 3
    why is wa[4] special? I guess that default is executed when letters are found but still it's unclear. Commented Feb 8, 2018 at 2:51
  • aggred with @coderredoc why wa[4] is special . Your default case only increment size not value of w[]. Commented Feb 8, 2018 at 2:57
  • i tried compiling this code and running the program and i'm infinitely stuck in the while loop. Commented Feb 8, 2018 at 2:58
  • oh wa[4] is just for test that the rest of the code is working ... to finish the White loop press ctrl+c in your console . Commented Feb 8, 2018 at 3:05
  • Please show a sample input and output, pointing out why you think the output is incorrect. Commented Feb 8, 2018 at 6:02

2 Answers 2

1
  • first, for testing purposes when running from Linux commandline Ctrl+d emulates EOF

    • second, your for loop iterates between ( 0 -10 inclusive ), your wa array index however is ranging from (0 - 9 inclusive ) which means:

      for(x=0;x

    call may cause SEGFAULT

    • third, you are missing a simple case where the input is just one word with no whitespace after, something like

    abcdEOF

    • fourth, following the second paragraph when entering valus to the array your indexing is wrong

    • as far as the assumption that the longest words is 10char long thats fine but you must verify that the size never exceeds the value of 9 or if you will correct the wa update then 10 exceeding this value will cause segfault due to updating un-allocated index in the array

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0

The first for loop will start from 1, and in the second for loop replace wa[4] with wa[r].

Your code also assume that no word will be longer than 10 char.

2 Comments

yes my mistake I used wa[4] to test if the rest of the code is working, the problem is that every loop I write after the while dont finish.
@JaredJosafhatHZ Do you have any indication that the loop even starts?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.