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

来源:互联网 发布:打车软件排行 编辑:程序博客网 时间:2024/04/29 22:53
#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";//2char *str = "  asc  12a  c";//2ret = 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;}


0 0
原创粉丝点击