#include <iostream>
#include <string>
#include <cctype>
size_t countwords(const char *);
using namespace std;
int main()
{
char a[] = "Four score and seven years ago";
cout << countwords(a);
return 0;
}
size_t countwords( const char *s )
{
size_t count = 0;
while ( *s )
{
while ( isspace( *s )) ++s;
if ( *s ) ++count;
while ( isalnum( *s )) ++s;
}
return ( count );
}
The only thing i'm missing and don't know how to perform is to print the length of each word in the sentence. I need my function to print the word and it's length side by side besides returning the word count. And also to print the average number of characters. so for example if the sentence was "four scores" I need:
Four 4
scores 5
average: 4.5