#include <iostream> #include <string> #include <cctype> #include <cstring> using
ID: 3691488 • Letter: #
Question
#include <iostream> #include <string> #include <cctype> #include <cstring> using namespace std; int main() { string fullname ("John William Jones"); cout << fullname; string firstname, lastname, middlename; /* Part 1. Using the string functions separate the first middle and last name from the fullname variable into the variables firstname, middlename and lastname. Print the first middle and last names on separate lines*/ string string1 ( "This is a string of characters for the optional quiz"); string string2; /* Part 2. Using the string functions remove all the blanks in string1 and and save the compressed string into a variable string2 and print the string2 on a new line. */ cout << " Optional Quiz" << endl; return 0; } Please complete in a way it can be copied and pasted to c++ codeblocks and show an image of the program being ran please
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
int main()
{
string fullname ("John William Jones");
int i = fullname.find(' ',0);
string firstname = fullname.substr(0,i);
int j = fullname.find(' ',i+1);
string middlename = fullname.substr(i,j-i);
string lastname = fullname.substr(j,fullname.length()-j);
cout<<firstname<<endl;
cout<<middlename<<endl;
cout<<lastname<<endl;
string string1 ( "This is a string of characters for the optional quiz");
string string2;
int k = string1.find(' ',0);
while(k!=-1)
{
string1.erase(k,1);
k = string1.find(' ',0);
}
string2=string1;
cout<<string2<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.