1. 程式人生 > >習題6-8 統計一行文字的單詞個數 (15 point(s))

習題6-8 統計一行文字的單詞個數 (15 point(s))

習題6-8 統計一行文字的單詞個數 (15 point(s))

本題目要求編寫程式統計一行字元中單詞的個數。所謂“單詞”是指連續不含空格的字串,各單詞之間用空格分隔,空格數可以是多個。

輸入格式:

輸入給出一行字元。

輸出格式:

在一行中輸出單詞個數。

輸入樣例:

Let's go to room 209.

輸出樣例:

5
#include<stdio.h>
int main(){
  char str[1001];
  gets(str);
  int count=0;
  int i=0;
  while(str[i]==' ')
  	i++;
  	while(str[i]!='\0'){
	  if(str[i]!=' '){
	  	count++;
	   	while(str[i]!=' '){
		   
	  		if(str[i]=='\0')
	  		break;
	  	 i++;
		  
	  }
	}
	  else
	  	i++;
	  
	  }
	  printf("%d",count);
	  
}