#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;
}
输出一行字符串中 所有纯英文单词的数目
最新推荐文章于 2024-04-26 21:01:08 发布