C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter7: User-defined Simple Data Types, Namespaces, And The String Type
Section: Chapter Questions
Problem 8PE: Write a program that reads in a line consisting of a students name, Social Security number, user ID,...
icon
Related questions
Question

C++

Write a function that accepts a string as argument and returns the number of words contained in the string. For instance, if the string argument is "Four score and seven years ago" the function should return the number 6 because there are six words in that string. Please use a meaningful word for the function name. Demonstrate the function in a program that prompts the user
to input a string then passes it to the function. The number of words in the string should be displayed on the screen. Declare the prototype for the function above the main and implement it below the main.
Hint: Use the getline(cin, str) function to read an entire line as input in the string variable str.
Partial Solution
Please try to implement the full source code in your IDE first. The solution is partially provided below. Fill in the blanks to complete the missing parts and make sure to not add an empty space before and after the answer.
#include <iostream>
//include the string library
#include <
using namespace std;
//prototype of the wordCount function
int wordCount (
int main()
}
int wordCounter = 0;
cout<<"Please input a line of string: ";
(cin, str);
wordCounter =
str;
//calling the wordCount function and return the value to an int variable
(str);
cout <<"\nThe input contains " <<
return 0;
aString);
}
//The function counts number of word from an input string
wordCount( string myString) {
int count = 0;
for (int i = 0; i < myString.
{
if( (i+1) <= myString.length()){
return
; i++)
<< " words.";
//3 boolean variables
bool isCurrentCharEmpty = (myString[i] == '');
bool isNextCharEmpty = (myString[i+1] == ' ');
bool isCurrentCharTheLast = (i+1== myString.length());
//check if the current character is any type of characters eg. A,a,1,?
//and the next char is an empty space
//or it is the last character
//then it is a word.
(!isCurrentCharEmpty && (isNextCharEmpty || isCurrentCharTheLast)){
count += 1; //increment the count by one
Transcribed Image Text:Write a function that accepts a string as argument and returns the number of words contained in the string. For instance, if the string argument is "Four score and seven years ago" the function should return the number 6 because there are six words in that string. Please use a meaningful word for the function name. Demonstrate the function in a program that prompts the user to input a string then passes it to the function. The number of words in the string should be displayed on the screen. Declare the prototype for the function above the main and implement it below the main. Hint: Use the getline(cin, str) function to read an entire line as input in the string variable str. Partial Solution Please try to implement the full source code in your IDE first. The solution is partially provided below. Fill in the blanks to complete the missing parts and make sure to not add an empty space before and after the answer. #include <iostream> //include the string library #include < using namespace std; //prototype of the wordCount function int wordCount ( int main() } int wordCounter = 0; cout<<"Please input a line of string: "; (cin, str); wordCounter = str; //calling the wordCount function and return the value to an int variable (str); cout <<"\nThe input contains " << return 0; aString); } //The function counts number of word from an input string wordCount( string myString) { int count = 0; for (int i = 0; i < myString. { if( (i+1) <= myString.length()){ return ; i++) << " words."; //3 boolean variables bool isCurrentCharEmpty = (myString[i] == ''); bool isNextCharEmpty = (myString[i+1] == ' '); bool isCurrentCharTheLast = (i+1== myString.length()); //check if the current character is any type of characters eg. A,a,1,? //and the next char is an empty space //or it is the last character //then it is a word. (!isCurrentCharEmpty && (isNextCharEmpty || isCurrentCharTheLast)){ count += 1; //increment the count by one
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Fundamentals of Boolean Algebra and Digital Logics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning