Ⅰ JAVA中,统计单词的个数,并按要求输出。
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "I am a student";
String[] arr = str.split(" ");
System.out.println("共"+arr.length+"个单词");
for(String item:arr) System.out.println(item);
}
Ⅱ C语言 统计文件中单词的个数,并将单词依次输出到屏幕
#include
#include
int main(void)
{
int a = 0, b = 0, c = 0;
char buf[128];
FILE *fp;
/* 打开文件,文件名必须大写 */
fp= fopen("DATA5610.TXT", "r");
if (!fp) {
printf("No 'DATA5610.TXT' found.\n");
return -1;
}
/* 逐次读取单词,空格或回车分割 */
while (fscanf(fp, "%s", buf) > 0) {
/* 如读取到的单词是 if,则a自增 1 */
if (strcmp(buf, "if") == 0)
a++;
else if (strcmp(buf, "while") == 0)
b++;
else if (strcmp(buf, "for") == 0)
c++;
}
printf("if: %d, while: %d, for: %d\n", a, b, c);
fclose(fp);
return 0;
}
Ⅲ Python里,输入一个英文句子,统计并输出单词个数,怎么弄啊,
txt='abcdefgh'
arr=txt.split()
print('单词数:',len(arr),arr)
#输出结果:
#单词数:5['a','b','c','de','fgh']
Ⅳ C语言编一个函数输出单词个数
偶也是初学者,在sizeof上始终都用不明白。。。。。程序有误,回仅仅做参考答。
#include
#include
int main()
{
char i;
int sum;
printf("Please enter: (enter to quit!)");
line:
scanf("%c",&i);
printf("%-2c\n",i);
sum = sizeof(i);
if(sizeof(i) > 1)
goto line;
printf("%d\n",sum);
exit(0);
}
Ⅳ C语言输入一段英文要求统计出这段英文的单词数,字符数,行数
#include
#include
voidmain()
{
intc,nl,nw,nc;
nl=0;//这里默认都是0;
nw=0;
nc=0;
while((c=getchar())!='@')
{
if(c=='
')//如果是
就把行数+1
专nl++;
if(!isalnum(c))//如果不属是单词就把单词数+1
nw++;
else//如果是单词就把字符数+1
nc++;
}
printf("character=%d
lines=%d
words=%d
",nc,nl,nw);
}
Ⅵ 从键盘输入一段英文,统计并输出单词个数
//有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数。
# include
# include
void main()
{
char ch[3][80];
cout<
for(int i=0;i<3;i++)
gets(ch[i]);
for(i=0;i<3;i++)
{
int x=0,y=0,z=0,m=0,n=0;
for(int j=0;j<80;j++)
if(ch[i][j]=='\0')
break;
else if('A'<=ch[i][j]&&ch[i][j]<='Z')
x++;
else if('a'<=ch[i][j]&&ch[i][j]<='z')
y++;
else if('0'<=ch[i][j]&&ch[i][j]<='9')
z++;
else if(ch[i][j]==' ')
m++;
else
n++;
cout<
}
}
呵呵,不是原题,纯参考,我做任务的,不想太糊弄,就给你段代码参考下……加油,自己写很容易的!!
Ⅶ c++ 输入一句英文句子 判断其中单词个数 并分别输出每个单词。
应用C++的string类对象实现。具体做法是:从键盘输入英文句子到string类对象s,然后遍历该对象(字符串),以字母开始以字母结束,中间只有字母和'-'的被认为是一个单词;在判断过程中把它们组装到另一个string类对象st中。此后再遇到不是字母或'-'时输出st(单词),输出后将st置空表示该单词已输出,并将单词计数器sum增1。举例代码如下://#include"stdafx.h"//Ifthevc++6.0,withthisline.
#include
#include
usingnamespacestd;
intmain(void){
stringst,s;
inti,j,k,ln,sum;
charch;
cout<
";
while(s+=(ch=cin.get()),ch!='
');//输入一个英文句子
cout<
for(sum=i=0,ln=s.length();i
if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z'||s[i]=='-')
st+=s[i];//以字母始字母终,中间只有字母和'-'的就组织成单词存入st
elseif(st!=""){
cout<
sum++;//单词个数增1
st="";//st置空,表示该单词已输出
}
}
cout<
Atotalof"<
";//最后输出单词个数
return0;
}
执行结果示例如下图:
Ⅷ C语言编一个函数输出单词个数
#include
#include
#include
int main()
{
char c;
int letters = 0;
bool lastInputIsSpace = false;
c = getch();
printf("%c", c);
while (c!='\r') {
if (c==' ') {
if (!lastInputIsSpace) {
letters++;
}
lastInputIsSpace = true;
}
else {
lastInputIsSpace = false;
}
c = getch();
printf("%c", c);
}
if (!lastInputIsSpace) {
//末尾只要不是空格就需要再增加内一个字符数容
letters ++;
}
printf("\n\nletters count is %d", letters);
system("pause");
return 0;
}
Ⅸ C语言统计单词个数
Q:输入一串字符串,输出其中有多少个单词。
Eg:Good Wishes
A:
#include #include #define SIZE 20int main()
{ char str[SIZE]={'