输出一行字符串中 所有纯英文单词的数目

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

//输出一行字符串中 所有纯英文单词的数目
int getWordsNum(char *mystr,int *ncount){

	int ret = 0;
	int tempCount = 0;
	char *temp = mystr;
	int  flag = 0;
	int  begin = 1;

	if (mystr == NULL || ncount == NULL){
		ret = -1;
		printf("func getWordsNum() err:%d",ret);
	}

	while (*temp!='\0')
	{
		//如果前一个字符是空格  后一个字符是字母开始判断
		if (!flag&&begin){
			begin = 0;
			if (*temp > 'A'&&*temp < 'z'){
				flag = 1;
				if (flag)
				{
					while (*temp > 'A' && *temp < 'z')
					{
						temp++;
					}
					如果前一个字符是字母  后一个字符是空格或'\0'  计数
					if (*temp == ' '|| *temp=='\0'){
						temp++;
						tempCount++;
					}
					flag = 0;
				}
			}
			
		}
		else
		{
			//如果字符串的第一个字符不是字母   从下一个空格处开始判断 
			while (*temp++ != ' ');
			
		}
		if (*(temp + 1) > 'A' && *(temp + 1) < 'z'){
			begin = 1;
		}
		temp++;
	}

	//一级指针间接赋值,是指针存在的最大意义
	*ncount = tempCount;
	return ret;
}
int main(){
	
	int ret = 0;
	int count = 0;
	//char *str = "I am a student of 23";//5
	//char *str = "I am a student22 of 23";//4
	//char *str = "I 12am a student22 of 23";//3
	//char *str = "12I 12am a student22 of 23";//2
	//char *str = "12I 12am a student22 of 23";//2
	char *str = "  asc  12a  c";//2
	ret = getWordsNum(str, &count);
	if (ret != 0){
		printf("func getWordsNum() err:%d", ret);
		return ret;
	}

	printf("count:%d\n", count);
	printf("hello...\n");
	system("pause");
	return ret;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

余莫星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值