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

编程统计字符串中单词的个数。要求:输入一字符串,长度小于50,统计其中包含多少个单词,单词之间用空格分隔开。例如,输入“I am a student”,输出4。

#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;
}

 

如果输入i am 4 years old,那么4也算单词吗