#2019120500001 读取单词个数(四种做法)

#include <cstdio>
#include <cstring>
using namespace std;
char s[105];
int nw=1,count=0;
//way1

int main( ){
	gets(s+1);
	s[0]=' ';
	int le=strlen(s+1);
	//printf("%d 
",le);
	for(int i=0;i<=le;i++){
		if(s[i]==' '){
			nw=1;
		}
		else{
			if(nw==1){
				nw=0;
				count++;
			}
		}
	//	printf("%d %d %c
",i,count,s[i]);
	}
	printf("%d",count);
	return 0;
}

void way2( ){
	gets(s);
	for(int i=0;i<=101;i++){
		if(s[i]==''){
			break;
		}
		if(s[i]==' '){
			nw=1;
		}
		else{
			if(nw==1){
				nw=0;
				count++;
			}
		}
		//printf("%d %d %c
",i,count,s[i]);
	}
	printf("%d",count);
	return ;
}
#include <cstdio>
#include <cstring>
#include <stack>

using namespace std;
char sm[105];
char str[105];
int cnt=0;
void way3(){
	 stack < int > sp;
	gets(sm);
	int le=strlen(sm);
	sp.push(0);//事先压入空格 
	for(int i=0;i<le;i++){
		if(sm[i]!=' ') sp.push(sm[i]-'0');
		else sp.push(0);//处理字符串 
	}
	while(sp.size()!=0){
		char tmp;
		tmp=sp.top();
		if(tmp==0) cnt++;
		sp.pop();//统计 
	}
	printf("%d",cnt);
	return ;
}
const int N=1e5+7;
char c[N];
char arr[N][1005];
char tmp[N];

void way4(){
	char *p=c;
	gets(c);
	int i=0;
	int ans=0;
	while(++i){
		while(p[0]==' ') p+=1;
		if(sscanf(p,"%s",arr[i])==-1)
		break;
		sprintf(tmp,"%s",arr[i]);
		p+=strlen(tmp);
		ans++;
	}
	printf("%d
",ans);
	return ;
}
要做就做南波万
原文地址:https://www.cnblogs.com/liuziwen0224/p/11991520.html