Using the find() function, how do I find the acronym of an inputted string? The program would find the first letter of each string, put all leading letters in one string, and display it. For example, an input of "this is a test input" would output "tiati". An input of "national aeronautics space administration" would output "nasa".
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
usingnamespace std;
int main() {
string text;
cout<<"Enter text";
getline(cin, text);
//using the find() function find the first letter of each string to find the acronym
cout<<"Acronym: ";
}
You might like stringstream as well if find is optional.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
string badd = "A Criminal Regiment of Nasty Young Men";
stringstream ss;
ss << badd;
string temp;
while(ss >> temp)
{
cout << temp[0];
}
cout << endl;
}
Otherwise print the first letter in the line, then use find to find the next whitespace and print the next letter after that. Rinse and repeat.
You should end this loop when the result is string::npos