㈠ 统计一行文本的单词个数:输入一行字符,统计其中单词的个数.个单词之间用空格分隔,空格数可以是多个,
代码部分: #include
int main()
{
int count=0;
char temp;
bool letter;
letter=0;//letter=0为在单词里(单词没输入完成),=1为表示进入下个单词
printf("Input words:");
temp=getchar();
while(temp!='\n')
{
if((letter==1)&&(temp==' '))
{
letter=0;
}
else if((temp>='a'&&temp<='z')||(temp>='A'&&temp<='Z')||(temp>='0'&&temp<='9'))
{
if(letter==0)
{
letter=1;
count++;
}
}
temp=getchar();
}
printf("count: %d\n",count);
return 0;
} 效果图: 【酷_酷_币】为您服务...
㈡ C语言 统计文件中各个单词的个数
你的程序只需要修改两处就行了:(1.)不能用指针数组直接存放字符串,即,要把程序开头的char *s2[30]改为char s2[30][30],用二维数组来存放多个字符串;(2.)你的想法是,如果单词不是重复的单词,就将它存入s2,但这时不能用“=”,应该用strcpy()函数,即,原程序中的s2[k]=s3应改为strcpy(s2[k],s3),改过后的代码如下:
#include
#include
int main()
{
char str[100],ch;
char s2[30][30]; /*s2存放单词(不重复),修改处*/
char s3[30],*p=s3;
int i,j,k=0,flag1,flag2,count1=0,white=1,a[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
FILE *file;
file=fopen("string.txt","w+");
printf("Input a string of English:\n");
gets(str);
fputs(str,file);
rewind(file);
while((ch=fgetc(file))!='0') /*统计文件中有多少个单词*/
{
if((ch==' ')||(ch=='\t')||(ch=='\n'))
white++;
else
{
if(white)
{
white=0;
count1++;
}
}
}
rewind(file);
for(i=0;i
{
flag1=0; /*是否有重复单词*/
flag2=0; /*是否遇到空白字符*/
p=s3;
while(ch=fgetc(file))
{
if((ch!=' ')&&(ch!='\t')&&(ch!='0'))
{
if((ch>64)&&(ch<91)) /*大写字母变小写*/
{
ch=ch+32;
}
*p++=ch;
}
else
{
flag2=1; /*遇到空白字符*/
*p='\0';
for(j=0;j
{
if(strcmp(s3,s2[j])==0)
{
flag1=1; /*有重复单词*/
a[j]++;
break;
}
}
if(flag1==0) /*如果单词不重复*/
{
strcpy(s2[k],s3); /*修改处*/
a[k]++;
k++;
}
}
if(flag2==1) /*如果遇到空白字符,则开始查询下一单词,跳出while循环*/
{
break;
}
}
while(((ch=fgetc(file))==' ')||(ch=='\t')); /*吞掉多余的空白字符*/
fseek(file,-1,1); /*后退一格,继续读*/
}
for(i=0;i
{
printf("%s\t",s2[i]);
}
printf("\n");
for(i=0;i
{
printf("%d\t",a[i]);
}
printf("\n");
fclose(file);
return 0;
}
明白了吗?
㈢ 用C++编程序统计文本中单词的个数
这麽理解:单词的个数 = 空格的个数+1,
连续的空格按1个算,如果有其他的逻版辑则还需要权进行处理,比如,.“的判断等等。
思路是: 循环读取文件直至为空,每次读取1024个字节到字符串数组filestr中
flag = false; -- -- false表示上一次读取到的字符不为单词的字母,用于屏蔽连续空格的干扰
num = 0; -- 空格的个数
for(i = 0; i < 1000 ; ++i)
{
if ( filestr[i] == ' ' && flag)
{
num++;
flag = false;
}
else
flag = true;
}
cout<
㈣ 统计一个文本文件里面各个单词出现的次数
用软件Replace Pioneer就可以,详细步骤:
1. ctrl-o打开文件
2. 打开Tools->Pattern Counter菜单,
3. Source选Current page, Counter template选Characters, Words, Lines
4. 选中内列表中Words这一行,点击detail按钮即可。
在官容网上可以搜索各种统计的例子。
㈤ vb统计一个文本文件中某个单词(英文字符)的数量
没有抄测袭试过。。。
Open "d:\123.txt" For binary As #1
S1 = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
S2="yes"
N=(len(S1) -len(replace(s1,s2,""))) / len(s2)
msgbox n
㈥ 统计某文本文件中各单词个数C语言设计
#include
#include
void main()
{
char ch;
int numberofword=0,wordStart=0;
FILE *fp1 = fopen("test.txt","r");
FILE *fp2 = fopen("result.txt", "w");
if( fp1==NULL || fp2==NULL )
{
puts("cannot open file!");
return;
}
while( !foef(fp1) )
{
ch =fgetc(fp1);
if( isalpha(ch) && wordStart==0 )
{
wordStart = 1;
}
else if( !isalpha(ch) && wordStart==1 )
{
numberofword++;
wordStart = 0;
}
}
fprintf(fp2,"%d",numberofword);
fclose(fp1);
fclose(fp2);
}
㈦ 如何统计一个文本中指定单词的个数
您好,我来为您抄解答:
我想找袭到所有以 unix 打头的字段,我用下面的语句
awk '{for (i=1;i<=NF;i++) if ($i ~ /^unix.*/ count++} END{print count}' a.txt
如果我的回答没能帮助您,请继续追问。
㈧ java统计txt文件中的单词数
你的程序我帮你改完了,你看看吧.(改动的地方见注释)importjava.util.Scanner;
importjava.io.File;
publicclassText{
publicstaticvoidmain(String[]args){
try{
Scannersc=newScanner(newFile("qia.txt"));
intwords=0;//这里把这句移到这专里
while(sc.hasNextLine()){
Stringstr=sc.nextLine();
Stringstr1=str.trim();
String[]xx=str1.split("
\s
+");
inta1=xx.length;
words=words+a1;
}
System.out.println("单词属数:"+words);//这里把这句移到这里,并输出words
}catch(Exceptionex){
}
}
}
㈨ 统计某文本文件中各单词个数
英文的话,一般是统计空格数。
中文嘛,文件的大小除以二吧。
混合的,根据编码将中英文区分分别统计即可。
㈩ 统计一个文本文件中单词的个数JAVA。 这图有对的地吗应该怎么写
先用io流按行读取文件内容,用string拼接一个长字符串,然后按空格截断,存入数组,最后输出数组长度即可