输入一行文本,按照相应格式输出每个单词的长度
#include <iostream> using namespace std; #include <string> #include <vector> #include <iomanip> int main(){ string sen = " qwe asd axs zfsfs ddwfa s"; //getline(cin,sen); 如果要把输入的空格的记录进string里面,就要用getline(cin,sen); vector<string> letter; int size = sen.size(); int left = -1; int i; for(i = 0;i < size;i++){ if(sen[i]>='a'&&sen[i]<='z' || sen[i]>='A'&&sen[i]<='Z'){ if(left == -1) {left = i;} } else if(left != -1){ letter.push_back( sen.substr(left,i-left) ); left = -1; } } if(left != -1) letter.push_back( sen.substr(left,i-left) ); cout << setw(8) << "word" << setw(8) << "lenth" << endl; for(int i = 0;i < letter.size();i++){ cout << setw(8) << letter[i] << setw(8) << letter[i].size() << endl; } }
结果
标签:16,int,真题,C++,单词,sen,include,size From: https://www.cnblogs.com/uacs2024/p/18088043