1) Retype and correct the code provided to combine two strings separated by a sp
ID: 3759579 • Letter: 1
Question
1) Retype and correct the code provided to combine two strings separated by a space. Hint: What type of parameter does push_back expect?
#include
#include
using namespace std;
int main() {
string secretID = "Barry";
string lastName = "Allen";
char spaceChar = ' ';
/* Your solution goes here */
cout << secretID << endl;
return 0;
}
2)Modify secondVerse to play "The Name Game" (a.k.a. "The Banana Song", see Wikipedia.org), by replacing "(Name)" with userName but without the first letter. Ex: if userName = "Katie", the program prints:
#include
#include
using namespace std;
int main() {
string secondVerse = "Banana-fana fo-f(Name)!";
string userName = "Katie";
userName.erase(userName.begin()); // Removes first char from userName
/* Your solution goes here */
cout << secondVerse << endl;
return 0;
}
3)Write a for loop to populate vector userGuesses with NUM_GUESSES integers. Read integers using cin. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int NUM_GUESSES = 3;
vector<int> userGuesses(NUM_GUESSES);
int i = 0;
/* Your solution goes here */
return 0;
}
Explanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
int main() {
string secretID = "Barry";
string lastName = "Allen";
char spaceChar = ' ';
/* Your solution goes here */
secretID.push_back(spaceChar);
secretID = secretID + lastName;
cout << secretID << endl;
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
#include<string.h>
using namespace std;
int main() {
string secondVerse = "Banana-fana fo-f(Name)!";
string userName = "Katie";
userName.erase(userName.begin()); // Removes first char from userName
/* Your solution goes here */
secondVerse = secondVerse.substr(0, secondVerse.length()-7);
secondVerse += userName;
cout << secondVerse << endl;
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int NUM_GUESSES = 3;
vector<int> userGuesses(NUM_GUESSES);
int i = 0;
/* Your solution goes here */
for (i=0; i<NUM_GUESSES; i++) {
int input;
cin>>input;
userGuesses.at(i) = input;
}
cout<<"The user guesses are: ";
for(std::vector<int>::iterator it = userGuesses.begin(); it != userGuesses.end(); ++it) {
cout<<*it<<", ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.