In: Computer Science
(For C++) Assume that sentence is a variable of type string that has been assigned a value . Assume furthermore that this value is a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence."
Assume that there is another variable declared , secondWord, also of type string . Write the statements needed so that the second word of the value of sentence is assigned to secondWord. So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Declaring the variable
string sentence,secondWord;
//Getting the sentence entered by the user
cout<<"Enter the sentence :";
std::getline(std::cin,sentence);
/* Getting the sentence after the first space
* and stored into the word secondWord
* (i.e here we are excluding the first word)
*/
secondWord = sentence.substr(sentence.find(" ")+1);
/* here now we are taking the word from
* the previous output sentenceWord till we get
space
*/
secondWord = secondWord.substr(0, secondWord.find(" "));
//Displaying the second word.
cout<<"The secondWord
:"<<secondWord<<endl;
return 0;
}
____________________
Output:

__________Thank You