编程统计字符串中单词的个数。要求:输入一字符串,长度小于50,统计其中包含多少个单词,单词之间用空格分隔开。例如,输入“I am a student”,输出4。
2条回答 默认 最新
- 只有我不在の星空 2021-05-31 20:24关注
#include<bits/stdc++.h> using namespace std; int main(){ int t=1; string a; getline(cin,a); int x=a.length(); for(int i=0;i<x;i++){ if(a[i]==' ') t++; } cout<<t; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报