Word Counter : Write a function that accepts apointer to a C-String as an argume
ID: 3618473 • 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.
Use this skeleton for 'main':
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;
}
Note: The main problem in this exercise istraveling within a string,
character by character, while determining whether we are inside ofa word or in
between words (space). Here is a bit of pseudocode, which shouldhelp get started:
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])) )
From the book:
Average Number of Letters: Modify the program youwrote for problem 3
(Word Counter), so it also displays the average number of lettersin each word.
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
#include<cstring>
int Word_count(char *);
//Main function
void main()
{//Start main
const int size=50;
char String1[size];//Holds string
int words;
//Inputting String
cout<<"Enter String ";
cin.getline(String1,size);
//Function call
words=Word_count(String1);
//Outputting Number of returnedword count
cout<<"Number ofwords:"<<words<<endl;
//Statement to Pause System
system("pause");
}//End of main
//Function Definition
int Word_count(char *str)
{
//Variable declaration
int count=1;//Holds wordcount
//Loop to count words
while(*str!='')
{
if(*str==' ')
count++;
str++;
}
//Returning count value tomain
return count;
}
//End of function
//Header file section
#include<iostream>
#include<cctype>
#include<cstring>
using namespace std;
//Function prototype
int Word_count(char *);
//Main function
void main()
{//Start main
const int size=30;
char String1[size];//Holds string
int Average_words;
//Inputting String
cout<<"Enter String up to(20)characters";
cin.getline(String1,size);
//Function call
Average_words=Word_count(String1);
//Outputting Number of returnedword count
cout<<"Number of Letters in Eachword:"
<<Average_words<<endl;
//Statement to Pause System
system("pause");
}//End of main
//Function Definition
int Word_count(char *str)
{
//Variable declaration
int count=1,average=0;//Holdsword count
//Loop to count words
while(*str!='')
{
if(*str==' ')
count++;
str++;
}
average=20/count;
//Returning count value tomain
return average;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.