Firstly I just want to say that I don't want any solution or anything that will even mean that I don't have to do some extra thinking. I'm just looking for a nod in the right direction.
I'm trying to implement a readability programme where I firstly need to prompt user for input and print the number of letters, words and sentences.
I've successfully completed the "letters" element using a for loop.
Code:
for (int c = 0, n = strlen(text); c < n; c++)
{
if (isalpha(text[c]))
{
count_letters++;
}
}
Now need to do the "words". I've tried lots of different things but had no luck.
For the purposes of this program, a word may be defined as the first character in a string, passing over the other characters until a *whitespace* which denotes a new word.
I believe that what I need to do is target specific elements in the array and test them using a function like
and/or However, since the array is created by the user's input, I simply won't be able, in advance, to know the length of the array.
The following code:
Code:
for (int d = 0, e = strlen(text); d < e; d++)
{
printf("%c\n", text[0]);
printf("%c\n", text[4]);
count_words++;
}
prints the first two letters (n and y) of the words I typed in (not you), so I know that the first letters exist at those points.
However, when I run:
Code:
for (int d = 0, e = strlen(text); d < e; d++)
{
if (isalpha(text[0]))
{
count_words++;
}
}
it prints "7 word(s)" (6 letters + 1 whitespace).
Sorry for long post!