Iterating through an array
I'm working on another program from my book and I've come across a little snag.
The goal of the program is to take in a sentence given by the user and count the number of words. (actually, it takes in the number of spaces between words up to the null character)
The problem I'm having is I'm not quite sure how to iterate through the array to count the characters themselves.
This is what Ive completed so far.
While you guys are looking at it, please feel free to critique the code. Lemme know what i could change to make it better.
Thanks guys.
Code:
//program to count words in a user supplied string
#include <stdio.h>
char line [100];
char sentence = 0;
char wordcount(char setence);
int numberOfWords = 0;
int main()
{
printf("Enter a line and I will count the number of words: ");
fgets(line, sizeof(line), stdin);
printf("There are %d words in the sentance.", numberOfWords);
}
//function: wordcount
//this function takes in the sentance variable, counts to each space character defined as ' ' and prints out the number of times
//it saw the ' ' (space) character.
char wordcount(char line)
{
for(line <= sizeof(line), line == "", ++numberOfWords)
{
return numberOfWords;
}
}