Given the following vector: vector<string> vocab; Write code that will output an
ID: 3838248 • Letter: G
Question
Given the following vector:vector<string> vocab;
Write code that will output any word in the vector that begins with the letter X, upper- or lower-case. Any other word that does not begin with X should not be printed. You may want to add words to test with, but your code to take care of the requirements should work no matter how many strings are in vocab.
For any word that starts with X the output should look like:
xylum begins with x.
Xylophone begins with X.
Xenon begins with X.
x-ray begins with x.
Note how the output shows whether the particular word begins with upper or lower-case X.
HINT: We went over a method that accesses a particular char in a string variable.
Given the following vector:
vector<string> vocab;
Write code that will output any word in the vector that begins with the letter X, upper- or lower-case. Any other word that does not begin with X should not be printed. You may want to add words to test with, but your code to take care of the requirements should work no matter how many strings are in vocab.
For any word that starts with X the output should look like:
xylum begins with x.
Xylophone begins with X.
Xenon begins with X.
x-ray begins with x.
Note how the output shows whether the particular word begins with upper or lower-case X.
HINT: We went over a method that accesses a particular char in a string variable.
Given the following vector:
vector<string> vocab;
Write code that will output any word in the vector that begins with the letter X, upper- or lower-case. Any other word that does not begin with X should not be printed. You may want to add words to test with, but your code to take care of the requirements should work no matter how many strings are in vocab.
For any word that starts with X the output should look like:
xylum begins with x.
Xylophone begins with X.
Xenon begins with X.
x-ray begins with x.
Note how the output shows whether the particular word begins with upper or lower-case X.
HINT: We went over a method that accesses a particular char in a string variable.
Explanation / Answer
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int main()
{
vector<string> vocab; // declare vector
string word;
bool more = true;
while (more) //enter strings in it using push_back
{
cout << " Please enter string, 0 to quit: ";
cin >> word;
if (word== "quit")
more = false;
else
vocab.push_back(word);
}
for (int i = 0; i < vocab.size(); i++)
cout<<" "<<vocab[i]<<" begins with "<<vocab[i].at(0)<<"."; //use of at() function
return 0;
}
Output:
Please enter string, 0 to quit: xylum
Please enter string, 0 to quit: Xylophone
Please enter string, 0 to quit: Xenon
Please enter string, 0 to quit: x-ray
Please enter string, 0 to quit: quit
xylum begins with x.
Xylophone begins with X.
Xenon begins with X.
x-ray begins with x.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.