标题 “hello everyone nihao!”判断该字符串有多少个单词
#include int main(int argc, char *argv[]){ char *str = "hello everyone nihao!"; int cnt = 0; int word_len = 0; while('\0' != *str) { //用空格区分单词 没有考虑特殊字符 if (' ' == *str) { //避免多个空格连在一起 if (word_len > 0) { cnt++; word_len = 0; } } //单词长度 避免多个空格 如果一个字符是不是不应该算单词? else { word_len++; } str++; } //最后一个单词之后大多是结束而没有空格 只能通过单词长度来区分是否有单词 if (word_len > 0) { cnt++; } printf("单词个数:%d
", cnt); return 0;}一个比较通用的单词个数统计,但没有考虑特殊字符,以空格区分单词
直接找不含exp字符的单词正则是实现不了的.
不过可以取巧,找到含exp字符单词,然后把这些单词替换成空字符串,然后再在替换好的字符串里面找单词
找含exp的单词的正则
(?=[A-Za-z]*exp[A-Za-z]*)[A-Za-z]*
匹配替换后的字符串的字符的正则
[A-Za-z]*
如图
#include <stdio.h>//c语言
#include <iostream>//c++
int main(){
char[] str = "hello everyone nihao!";
int count = 0;//用于统计空格个数以此来计算单词数量
if(str[0] == 0){//如果字符串为空
fprintf("单词个数为%d",count);//c语言
std::cout<<"单词个数为"<<count<<endl;//c++
return 0;
}
count++;//若字符串不为空则默认至少有一个单词
for(int i=0;;i++){
if(str[i] == 0)//字符串结束标志的ASCII码的值为0
break;
if(str[i] == ' ')//若遇到空格字符
count++;
}
fprintf("单词个数为%d",count);//c语言
std::cout<<"单词个数为"<<count<<endl;//c++
return 0;
}
如果是java的话
class Solution {
public static void main() {
String str = "hello everyone nihao!";
if (str.isEmpty()) {// 若字符串为空
System.out.println("单词个数为0");
return;
}
System.out.println("单词个数是:" + str.split(" ").length);
}
}
如果是C#的话跟java差不多
using System;
class Solution {
public static void main(string[] args) {
string str = "hello everyone nihao!";
if (str.length()==0) {// 若字符串为空
Console.WriteLine("单词个数为0");
return;
}
Console.WriteLine("单词个数是:" + str.split(' ').length);
}
}
Python
if __name__ == "__main__":
str = "hello everyone nihao!"
if str=="":
print("单词个数为0")
print(len(str.split(' ')))
hello.hell.he.every.ever.very.one.on.everyone.9个单词 nihao是拼音,不算单词。
两个啊。“hello”和“everyone”。😂
用sizeof()运算符不过得减去3,因为有一个是空字符,和两个空格。或者用strlen()函数,减去2,因为有2个空格。
满意请采纳!谢谢
你要判断这个必须建立一个英语词典数据库。根据单词使用量和使用范围进行比对。没有简洁方法。除非你和有翻译软件的翻译公司有一腿,让他们给你开个服务器后门。
同学们好怎么用英语表达
1、Hello,boys and girls.翻译:同学们好。2、Hello, students.翻译:同学们好。3、Hello everyon.翻译:同学们好;大家好。4、Hello,class.翻译:同学们好。5、Hello,everybody.翻译:同学们好。
同学们好英语
1、Hello,boys and girls.翻译:同学们好。2、Hello, students.翻译:同学们好。3、Hello everyon.翻译:同学们好;大家好。4、Hello,class.翻译:同学们好。5、Hello,everybody.翻译:同学们好。
各位同学们用英语怎么说
1、Hello,boys and girls.翻译:同学们好。2、Hello, students.翻译:同学们好。3、Hello everyon.翻译:同学们好;大家好。4、Hello,class.翻译:同学们好。5、Hello,everybody.翻译:同学们好。
谁能用英语介绍一张全家福?
Hello, everyonr. This is a photo of my family. Look! This is my father and this is my mother. The boy is my sister. Do you know who is the girl? Is she my sister? No! Is she my cousin? No! Ha Ha! It's me. And these are my grandparents. It is a nice photo...
谁能用英语介绍一张全家福?
Hello, everyonr. This is a photo of my family. Look! This is my father and this is my mother. The boy is my sister. Do you know who is the girl? Is she my sister? No! Is she my cousin? No! Ha Ha! It's me. And these are my grandparents. It is a nice photo...
英文演讲结束&开始语。。急~~~
英文演讲开始语 ladis and gentalmen, good morning hello everyonr and welcome it's my great horner to be here i'm happy to see you all thank you all for being here 结束 i hope you all enjoy yourselves today thank you for listening thank you for your time thank you very much...
初中英语作文 Book is my best friend
hello,I'm a student in NO.2 middle school.I like playing sports,listen to music,but I like reading best.The book gives me a lot of fun,the book,let me also add a lot of knowledge,of course,not everyon...