There's a number of errors in your code. First off, your while loop will only write length values to the length array at its last index. Do you know why? Secondly, your conditional statements within the loop most certainly are not doing what you want them to. Can you explain, in words, what your program has to do in the loop to count the length of each word it encounters in the string? Can you write that in psuedocode?
Side note, I'm not sure if this was intentional but you're making a finite state machine. If it wasn't intentional, I recommend Googling "finite state machine", you'll find them very helpful.
#include <stdio.h>
int main()
{
int c, i, j, word, inspace;
word = 0;
inspace = 0;
int length [100];
for (i=0; i<100; ++i)
length[i] = 0; // initialize elements
i = 0;
while ((c = getchar()) != EOF && word<=100) {
if (c != ' ' && c != '\t' && c != '\n')
++length[i];
inspace = 1;
if (c == ' ' || c == '\t' || c == '\n')
{
if (inspace = 1)
inspace = 0;
--length[i];
++word;
}
printf( "histogram:");
for (j = 0; j < length[i]; ++j)
printf("*");
printf("\n");
}
}
The logic is pretty simple : If we encounter a non-whitespace character we in a word array and increment it. Else we are out of the word and decrement it.
Try running the logic you just described in your head. How many different lengths will that keep track of? Will those length(s) actually be the lengths of the words in the string?
Also, you need to put brackets {...} around your conditional statements if they're going to have more than one line in them. And on line 28 you used an assignment operator instead of comparison operator (==)
You're definitely getting there. Look at your code, is the inspace variable really necessary? (hint: Do you use it anywhere except where you're assigning it?). Also, should you increment the length variable when you hit white space or non white space?
Maybe you'll benefit more if I explain how I would do it, in words. I would have an array of int's, like you have now. Each index of the array would represent the length of the n'th word. In addition to the array, I would have 2 separate variables. One would store the amount of words found, therefor it would start at 0 and be incremented each time whitespace is hit. The second variable would store the length of the current word and would be incremented each time a non-whitespace character is hit. When whitespace is hit, it's value would be copied into the the index of the array given by the first variable (before the first variable is incremented) then reset to 0. When finished, you would have an array whose elements store the lengths of the words, in the order that words in the input are, and a variable indicating how many words were in the input (and thus, which index to go up to when outputting the results).
On line 14 you need brackets around those 2 statements that I'm assuming you want in the for loop. Line 40 does nothing at all, as you're using the comparison operator but then never using the result of the comparison. Look carefully at your code, does it do what my previous post outlines? Do I ever mention a state variable?
#include <stdio.h>
int main() {
int lengths[20];
int currentWord = 0;
int currentWordSize = 0;
char c;
while ((c = getchar())!=EOF && currentWord<20) {
if (c==' ' || c=='\t' || c=='\n') {
//we've hit the end of the word. Set lengths[currentWord] to the counted word length
//then reset the counted word length to 0 and increment the current word
}
else {
//we're in a word, increment the current word length
}
}
//now loop through the lengths array in the range [0,currentWord) and output
return 0;
}