python求单词个数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python求单词个数相关的知识,希望对你有一定的参考价值。

参考技术A #s="Hello,my name is Jim Green and you?"
s=input("输入英文句子:")
lens=len(s) #字符总数
listb = []
js = 0
for j in s:
'''(Tab)处作缩进代码处理'''
(Tab)if j in [",", ".", "!", "?"]:
(Tab)(Tab)js += 1 # 标点符号计数
(Tab)(Tab)j = " "
(Tab)(Tab)listb.append(j)
(Tab)else:
(Tab)(Tab)listb.append(j)
ss = ("".join(listb))
ssl = ss.split(" ")
lenssl = len(ssl)+1-js #单词总数
#print(f"s\nss")
print(f"lens#lenssl")
'''运行效果
输入英文句子:Hello,my name is Bob.
21#5
'''

C语言 统计歌词中的单词个数,并找出最短和最长的单词(必须用指针)求帮忙,感谢!!!

统计歌词中的单词个数,并找出最短和最长的单词。
歌词:Daddy finger, Daddy finger, where are you? Here I am. Here I am. How do you do? Mammy finger, Mammy finger, where are you? Here I am. Here I am. How do you do? Brother finger, Brother finger, where are you? Here I am. Here I am. How do you do? Sister finger, Sister finger, where are you? Here I am. Here I am. How do you do? Baby finger, Baby finger, where are you? Here I am. Here I am. How do you do?
设计函数实现:
函数int gecifun( char *geci, char *strmax, char *strmin)——实现求geci中的单词个数并返回,strmax和strmin中分别存放最长和最短单词;
主函数——调用函数gecifun并输出。
程序输出:歌词中共有85个单词。
歌词中最长的单词为Brother,最短的单词为I。

参考技术A 函数int gecifun( char *geci, char *strmax, char *strmin)——实现求geci中的单词个数并返回,strmax和strmin中分别存放最长和最短单词;
主函数——调用函数gecifun并输出。
参考技术B #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
int gecifun(char *geci, char *strmax, char *strmin)

char temp[30];//存储每个单词数组
int num=0,i,word=0;//单词数量和每个单词数组下标
for(i=0;(geci[i])!='\0';i++)

if(geci[i]==' '||geci[i]==',' || geci[i]=='.' || geci[i]=='?') //单词分隔符


if (word != 0)
temp[word]='\0';
if (num==1)//获取第一个单词的时候,将第一个单词复制给最大和最小
strcpy(strmax,temp);
strcpy(strmin,temp);

else//第二个单词之后比较字符串长度之后进行复制


if (strlen(temp)>=strlen(strmax))
strcpy(strmax,temp);
if (strlen(temp)<=strlen(strmin))
strcpy(strmin,temp);

num++;//单词数量递增
word = 0;//单词数组下标置零


else
temp[word++] = geci[i];//每个单词按位置赋值

return num;

int main()

int acount;
char lstr[30],sstr[30];
char song[N] = "Daddy finger, Daddy finger, where are you? Here I am. Here I am."
"How do you do? Mammy finger, Mammy finger, where are you? Here I am. Here "
"I am. How do you do? Brother finger, Brother finger, where are you? Here "
"I am. Here I am. How do you do? Sister finger, Sister finger, where are you?"
"Here I am. Here I am. How do you do? Baby finger, Baby finger, where are you?"
"Here I am. Here I am. How do you do?";
acount=gecifun(song,lstr,sstr);
printf("歌词中共有%d个单词。歌词中最长的单词为%s,最短的单词为%s。\n",acount,lstr,sstr);
return 0;

以上是关于python求单词个数的主要内容,如果未能解决你的问题,请参考以下文章

求字母的个数 求单词的个数

C语言 统计歌词中的单词个数,并找出最短和最长的单词(必须用指针)求帮忙,感谢!!!

输入一个字符串,统计其中包含的单词个数,假设单词与单词之间用空格分隔。

Python中统计一个文档中单词的个数

python统计单词中字母个数

python统计个单词数目