本题目要求编写程序,统计一行字符中单词的个数,并将每个单词的最后一个字符输出。所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个。
输入格式:
输入给出一行字符。
输出格式:
在第一行输出每个单词的最后一个字符。中间没有空格。 在第二行输出单词的个数。
输入样例:
How are you?
输出样例:
we?
3
就是非得用vector
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
//格式错误*5
//纯空格,连续空格,前边空格,后边空格
//没有单词也输出两行
//鬼哦
string s;
getline(cin,s);
vector<string> a;
int f1=0,f2=s.find(" "),i=0;
while(f2!=string::npos)
{
string s2;
s2.append(s,f1,f2-f1);
if(s2!=" "&&s2!="")
{
a.push_back(s2);
cout<<a[i][a[i].length()-1];
i++;
}
f1=f2;
f2=s.find(" ",f2+1);
}
string s2;
int flag=0;
s2.append(s,f1,s.length()-f1);
if(s2!=" ")
{
a.push_back(s2);
cout<<a[i][a[i].length()-1]<<endl;
i++;
}else{
flag=1;
cout<<endl;
}
if(i==0&&flag==0){
cout<<endl;
}
cout<<i<<endl;
return 0;
}