#include <iostream>
#include <string> // Note: This library is needed to use the string type
using namespace std;
int main() {
string userInput;
cout << "Enter text: " << endl;
getline(cin, userInput);
cout << "You entered: " << userInput << endl;
if (userInput.find("BFF,0")) {
cout << "BFF: best friend forever" << endl;
}
if (userInput.find("IDK, 0")) {
cout << "IDK: I don't know" << endl;
}
if (userInput.find("JK")) {
cout << "JK: just kidding" << endl;
}
if (userInput.find("TMI")) {
cout << "TMI: best friend forever" << endl;
}
if (userInput.find("TTYL")) {
cout << "TTYL: too much information" <<endl;
}
return 0;
}
Using arrays, then possibly something like this - where it is easier to add new acronyms/phrases:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <string>
#include <cctype>
#include <array>
#include <algorithm>
#include <iterator>
constexpr std::array acronym { "BFF", "IDK", "JK", "TMI", "TTFN" };
constexpr std::array expanded { "best friend forever", "I don't know", "Just kidding", "too much information", "ta ta for now\n" };
static_assert(acronym.size() == expanded.size());
void toUpper(std::string& str) {
for (auto& ch : str)
ch = static_cast<char>(toupper(static_cast<unsigned char>(ch)));
}
int main() {
std::string userInput;
std::cout << "Enter text: ";
std::getline(std::cin, userInput);
std::cout << "You entered: " << userInput << '\n';
toUpper(userInput);
if (const auto fnd { std::ranges::find(acronym, userInput) }; fnd != acronym.end())
std::cout << userInput << ": " << expanded[std::distance(acronym.begin(), fnd)] << '\n';
}
| |
Last edited on