Question
Problem Statement Write a function that accepts a string as argument and output the last character of all words contained in the string. For instance,
Problem Statement
Write a function that accepts a string as argument and output the last character of all words contained in the string. For instance, if the string argument is "Four score and seven years ago" the function should printout "r e d n s o" 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 last character 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
using namespace std;
( string myString ) int main() { stringInput; cout<<"Please input a line of string: "; (cin, ); //Call the print function ( );
return 0; }
void printAllFirstChar( string myString ) { int count = 0; for( int i = 0; i < myString. ; i++ ) { if( (i+1) <= myString.length() ){ //3 boolean variables //check if the current character is any type of characters eg. A,a,1,? bool isCurrentCharEmpty = (myString.at(i) == ' '); //and the next char is an empty space bool isNextCharEmpty; if( (i+1) < myString.length()){ isNextCharEmpty = (myString.at(i+1) == ' '); } //or it is the last character bool isCurrentCharTheLast = (i+1 == myString.length()); //then it is a word if( !isCurrentCharEmpty && (isNextCharEmpty || isCurrentCharTheLast)){ //print the first char cout << ; } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started