C課程練習-單詞長度-多一種思考

#include <stdio.h>
int main()
{ 
	char s[100]="";	//The way you get the string decides if you can choose traversal operators. 
	// while you have known that its end is '.', and you should choose to judge the string's element one by one. 
	gets(s);
	int i,j,k,count=0;
	for(k=0;k<100;)
	{
		for(i=k;i<100;i++)
		{
			if(s[i]==' '||s[i]=='.')
				break;	
			count++;
		}
		if(count!=0&&s[i]!='.')
		{
			printf("%d ",count);
			count=0;
		} 	
		else
		{
			printf("%d",count);
			count=0;
		}
		if(s[i]=='.') break;
		k=++i;	
	}
	return 0;
}

And the follwing one is the right edition. "Scanf " it noe by one.

#include <stdio.h>
int main()
{ 
	char s;
	int i,count;
	do
	{
		scanf("%c",&s);
		if(s=='.')  
		{
			if(i)
				printf("%d",i); 
		}
		if(s==' ')
		{
			if(i)
			{
				printf("%d ",i);
				i=0;
			}
		}
		else
			++i;
	}while(s!='.');
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章