Word Counter : Write a function that accepts apointer to a C-String as an argume
ID: 3618668 • Letter: W
Question
Word Counter: Write a function that accepts apointer to a C-String as an
argument and returns the number of words contained in the string.For instance,
if the string argument is "Four score and seven years ago" thefunction should
return the number 6. Demonstrate the function in a program thatasks the user to
input a string and then passes it to the function. The number ofwords in the
string should be displayed on the screen.
int main()
{
char cstring[81];
cout << " Enter a string, 80 or fewer characters: ";
cin.getline(cstring, 81);
cout << " The number of words in that string: ";
cout << wordCount(cstring) << endl << endl;
return 0;
}
index = 0
while string[index] != NULL
if char = space
while char = space
keep looping
if char = nonspace
word++ //we must be in a word
while char = nonspace
keep looping
index++
endwhile
Notes:
- The pseudocode above is not a required skeleton; it is onlymeant to
exemplify the concept. - Also don't forget that a pointer to the string will be used,inside the
wordCount function. - Be sure to use the function 'isalnum' and 'isspace' forcomparing
characters. - Anticipate possible multiple spaces in front of the sentence,in between
words, etc. - When looping inside of a word, be prepared to run into anapostrophy or
other punctuation (ex: don't), therefore, requiring an additionalcondition for
the while loop such as:
while( isalnum(word[index]) || ( ispunct(word[index])) )
Average Number of Letters: Modify the program youwrote for problem 3
(Word Counter), so it also displays the average number of lettersin each word.
Explanation / Answer
please rate - thanks with # 4 #include int wordCount(char[],int&); using namespace std; int main() {int tot=0,w; char cstring[81]; coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.