Given a string from the user, write code to print the string in reverse order
Solution #2 (with append, requires a bit extra addition)
string sentence;getline(cin, sentence);string newSentence;for (int i = sentence.length(); i > 0; i--) { newSentence.append("" + sentence[i - 1]);}cout << newSentence;
Solution #2 (with push_back, similar to vector)
string sentence;getline(cin, sentence);string newSentence;for (int i = sentence.length(); i > 0; i--) { newSentence.push_back(sentence[i-1])}cout << newSentence;
CHALLENGING: Prompt the user for a sentence and count the number of vowels (a, e, i, o, u) that appear in it
Solution #3
string sentence; getline(cin, sentence);vector<string> vowels = {"a", "e", "i", "o", "u"};for (string vowel : vowels) { int count = 0; for (int i = 0; i < sentence.length(); i++) { // string[0] converts the single letter in string literal into char char lowerCaseLetter = (char) tolower(sentence[i]); if (sentence[i] == vowel[0] || lowerCaseLetter == vowel[0]) { count += 1; }; } cout << "Found the vowel " << vowel << ": " << count << " time(s)" << endl;}
Have the user type two words, then display them joined together with a dash (for example: “hello” and “world” becomes “hello-world”).
Solution #4 (with append)
string a, b;cout << "Enter two words: ";cin >> a >> b;cout << a.append("-").append(b);
Solution #4 (with insert)
string a, b;cout << "Enter two words: ";cin >> a >> b;cout << a.insert("-").insert(b);
Additional questions
Write code that asks the user for a string and prints whether it begins with the letter ‘C’. Display “Yes” or “No”.
Given a string entered by the user, count and print how many times the character ‘e’ appears in it.
Ask for a string input and then print the string in all uppercase letters (do NOT use transform).
// Must have #include <cctype> before the int main() because toupper() is only referenceable from cctype library// #include <cctype>string input;getline(cin, input);for (size_t i = 0; i < input.length(); i++) { input[i] = toupper(input[i]);}cout << "Uppercase: " << input;
Prompt for a short sentence and print only the first word (the characters before the first space).
Vector QuestionsTo be worked on in the study group
Ask the user to enter 5 integers, store them in a vector, and then display the numbers in the same order.
vector<double> numbers = {};cout << "Enter 5 integers (one at a time): " << endl;for (int i = 0; i < 5; i++) { double num; cin >> num; numbers.push_back(num);}cout << "Listed all the numbers" << endl;for (double num : numbers) { cout << num << endl;}
Read 5 numbers into a vector, then print the sum of the numbers using a loop.
Given a vector of integers, find and display the largest value stored in the vector.
Ask the user to enter words one at a time, storing each input into a string vector, until the user types “done”; print all words the user entered.
Wasn’t discussed thoroughly during study session
vector<string> listOfInputs = {};while (true) { string word; cout << "Enter a word: "; cin >> word; if (word == "done") break; listOfInputs.push_back(word);}cout << "List of words: ";for (int i = 0; i < listOfInputs.size(); i++) { cout << listOfInputs[i]; if (i != listOfInputs.size() - 1) cout << ", ";}
Additional vector questions
Read 4 numbers into a vector and print out the smallest value.
Create a vector of size 5. Ask the user to enter all five values, then print them in reverse order.
vector<float> nums;float a, b, c, d, e;cout << "Enter five values: ";cin >> a >> b >> c >> d >> e;string message = "";for (int i = 5; i>0; i--) { message.append();}
Use a vector to store words entered by the user, then print only those words that begin with ‘a’.
Ask the user to input 5 numbers, store them in a vector, and print their average (mean value as a double).
Practice questions
Find the maximum element in a vector without using built-in functions.