秃头少年在线秃头 2021-05-31 19:59 采纳率: 66.7%
浏览 32
已采纳

求求C语言代码,感谢大佬

编程统计字符串中单词的个数。要求:输入一字符串,长度小于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;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?