Tuesday 15 July 2014

The C programming Language excercise 1 - 13 -


I'm going though though by "C programming language" & amp; Amp; R. I am trying to exercise 1 to 13 so that it can get the histogram of the length of the input words. The following is what I have tried to do. I can not find the last word length. How can I fix it?

  #define space '' #define maxLength 100 #include & lt; Stdio.h & gt; Int main () {int c, results [maxLength], count, I, maxWordLength; Count = maxWordLength = 0; For (i = 0; i & lt; max length; ++ i) result [i] = 0; While ((c = getchar ())! = '0') {if (! ((C == space) || (c == '\ t') || (c == '\ n'))) ++ count; Other {++ results [count]; If (calculation> maximum word length) maxWordLength = calculation; Calculation = 0; }} For (i = 1; i & lt; = maxWordLength; ++ i) printf ("\ n% d \ t% d \ n", i, result [i]); Return 0; }   

The following is my input

  1 12 123 1234 12345 1234560   

And the output comes like this, It is not giving the length of the last word

  1 1 2 1 3 1 4 1 5 1 I have understood why this happens. But how do I fix it?   

When you reach 0 the length of the last word of the histogram Without adding, you get the input out of the loop. A potential fix is ​​adding it after the loop:

  ...} / * Take the last word * / ++ results [count] in the account; If (calculation> maximum word length) maxWordLength = calculation;   

It certainly assumes that 0 indicates the end of character input and is not part of data.

No comments:

Post a Comment