/*
Name: 查找单词的个数
Copyright:
Author:
Date: 23/06/20 21:35
Description: 使用数组和字符串完成
*/
#include<bits/stdc++.h>
using namespace std;
int find_word(char str[]);
int main()
{
int n=0;
cin>>n;
getchar();
char word[n]={};
gets(word);
cout<<find_word(word);
return 0;
}
int find_word(char str[])
{
int ans=0;
int i=0;
bool blank = 0;//blank用来标记字母前面是否有空格,默认为0,是为了找到第一个字母,就默认前面是空格。
for(i=0;i<strlen(str);i++)
{
if(str[i] == ’ ')//判断当前字符是不是空格,空格的话标记为0
{
blank = 0;
} //如果不是空格,那就是字符,看看字符前面是不是空格,如果是我们就找到了第一个单词了。
else
{
if(blank==0)
{
blank=1;
ans++;
}
}
}
return ans;
}
查找字符串中的单词数目
最新推荐文章于 2022-02-20 16:47:12 发布