题解 | 简写单词
简写单词
https://www.nowcoder.com/practice/0cfa856bf0d649b88f6260d878f35bb4
#include <iostream> #include <sstream> #include <cctype> // toupper using namespace std; int main() { string line; getline(cin, line); // 读取整行输入 stringstream ss(line); //ss 就是 stringstream 类型的变量,像操作输入流 cin 一样,把字符串 line 按空格分割成一个个单词。 string word; string abbr = ""; while (ss >> word) { abbr += toupper(word[0]); // 取首字母并转为大写 } cout << abbr << endl; return 0; }
#include <sstream> #include <string> #include <iostream> using namespace std; int main() { string line = "Hello World 123"; stringstream ss(line); // 把字符串放入stringstream string word; while (ss >> word) { // 按空格依次提取单词 cout << word << endl; } return 0;