输入一个字符串,判断里面有多少个单词

#define _CRT_SECURE_NO_WARNINGS
#define StrLength 100

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


//输入一个字符串,判断里面有多少个单词
void main(void)
{
	char Str[StrLength];
	int i=0,Words=1,length=0;
	printf("请输入一个字符串:\r\n");
	gets(Str);
	for (i = 0; i < StrLength; i++)
	{
		if(Str[i]=='\0')
		{
			break;
		}
		else
		{
			if(Str[i] == ' ')
			{
				Words++;
			}
		}
		length++;
	}
	if(length==0 )
	{
		Words=0;
	}
	printf("这个字符串里有%d个单词!\r\n",Words);
	system("pause");
}

猜你喜欢

转载自blog.csdn.net/kesshei/article/details/81352089