c语言程序设计:1,统计英文文本中单词个数。2,统计某一特定单词出现的频度。

有点急。。。。。。。。

1、统计英文文本中单词个数。

if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z')) sum++;

2、统计某一特定单词出现的频度。

for(i=0;i!='/0';i++)

{

if(a[i]=='特定单词')

sum++;

}

扩展资料:

if语句的一般形式如下:

if(表达式)语句1

[else语句2]

if语句中的“表达式”可以是关系表达式、逻辑表达式,甚至是数值表达式。其中最直观、最容易理解的是关系表达式。所谓关系表达式就是两个数值进行比较的式子。

for循环小括号里第一个“;”号前为一个为不参与循环的单次表达式,其可作为某一变量的初始化赋值语句, 用来给循环控制变量赋初值。

参考资料来源:百度百科-for循环

参考资料来源:百度百科-if语句

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-06
#include <stdio.h>
#include <stdlib.h>
#include "string.h"

typedef struct{
char word[20];
int count;
}WORD;

int find(char* source, char target)
{
int i,j;
int s_len=strlen(source);
for(i=0;i<s_len;i++)
{
if(source[i]==target)
{
return i;
}
}
return -1;
}

char* right(char* source,int n)
{
char* buf;
int len=strlen(source);
buf=(char*)malloc(sizeof(char)*(len-n)+1);
int i,j=0;
for(i=n;i<len;i++)
{
buf[j++]=source[i];
}
buf[j]='\0';
return buf;
}

void del_double_space(char* s)
{
int i,j=0,len=strlen(s);
for(i=0;i<len;)
{
s[j++]=s[i++];
if(s[i]==' ' && s[i-1]==' ')
{
i++;
}
}
s[j]='\0';
len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]>='A' && s[i]<='Z')
{
s[i]=s[i]+32;
}
}
}

char* left(char* source,int n)
{
char* buf;
buf=(char*)malloc(sizeof(char)*n+1);
int len,i,j=0;
len=strlen(source);
for(i=0;i<n;i++)
{
if(source[i]!='\0')
{
buf[j++]=source[i];
}
else
{
break;
}
}
buf[j]='\0';
return buf;
}

void sort(WORD* w)
{
WORD t;
int count=0;
int i,j;
for(i=0;i<100;i++)
{
if(w[i].count>0)
{
count++;
}
}
for(i=0;i<count;i++)
{
for(j=0;j<count-1-i;j++)
{
if(w[j].count<w[j+1].count)
{
t=w[j];
w[j]=w[j+1];
w[j+1]=t;
}
else if(w[j].count==w[j+1].count)
{
if(strcmp(w[j].word,w[j+1].word)>0)
{
t=w[j];
w[j]=w[j+1];
w[j+1]=t;
}
}
}
}
}

int main(int argc, char *argv[]) 
{
char all[1000]={'\0'};
while(1)
{
strcpy(all,"\0");
printf("\n\n请输入一段英文文字:");
fflush(stdin);
scanf("%[^\n]",all);
WORD word[100];
int i,duan=0,len,count=0;
len=strlen(all);
for(i=0;i<100;i++)
{
strcpy(word[i].word,"\0");
word[i].count=0;
}
for(i=0;i<len;i++)
{
if(all[i]==',' || all[i]=='.' || all[i]=='!' || all[i]=='?' || all[i]==';')
{
duan++;
if(i==len-1)
{
all[i]='\0';
}
else
{
all[i]=' ';
}
}
else if(i==len-1)
{
duan++;
}
}
del_double_space(all);
while(1)
{
char *buf,*newbuf;
int n;
buf=(char*)malloc(sizeof(char)*20);
newbuf=(char*)malloc(sizeof(char)*1000);
n=find(all,' ');
if(n!=-1)
{
buf=left(all,n);
}
else
{
strcpy(buf,all);
}
// printf("\n%s",buf);
int isfind=0;
for(i=0;i<count;i++)
{
if(strcmp(word[i].word,buf)==0)
{
word[i].count=word[i].count+1;
isfind=1;
break;
}
}
if(isfind==0)
{
strcpy(word[count].word,buf);
word[count].count=1;
count++;
}
if(find(all,' ')==-1)
{
free(buf);
free(newbuf);
break;
}
newbuf=right(all,n+1);
strcpy(all,newbuf);
free(buf);
free(newbuf);
// printf("\n%s",all);
// getch();
}
sort(word);
printf("\n共有%d句话",duan);
printf("\n共有%d个单词",count);
printf("\n单词       词频");
for(i=0;i<count;i++)
{
printf("\n%-10s    %d",word[i].word,word[i].count);
}
printf("\n\n是否继续?[y/n]");
char n;
fflush(stdin);
scanf("%c",&n);
if(n=='y' || n=='Y') 
{
continue;
}
else if( n=='n' || n=='N')
{
break;
}
}
return 0;
}

第2个回答  推荐于2018-06-19
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
FILE *f;
char x,y,a='a',A1='A',check;
int c=0,c1 = 0,c2 = 0,c3 = 0,i;
int data1[27];
int data2[27];
memset(data1,0,27*sizeof(int));
memset(data2,0,27*sizeof(int));

f = fopen("story.txt", "r");
if (f == NULL)
{
printf("Can't open the file!\n");
}
else
{
printf("是否区分大小写?Y or N\n");
scanf("%c",&y);
while (fscanf(f, "%c", &x), !feof(f))
{
if( (x>='a' && x<='z') || (x>='A' && x<='Z'))
{
c++;
if(x>='a'&&x<='z')
data1[x-'a']++;
else
{
if(y=='Y')
data2[x-'A']++;
else
data1[x-'a']++;
}
}
c1++;
}
fclose(f);
printf("字母出现的频率为:%.2lf%%\n",100.0*c/c1);
printf("请你输入要查找的字母\n");
scanf("%c",&check);
if(y=='Y')
{
        if(check>='a' && check<='z')
        printf("%.2lf%%\n",100.0*data1[check-'a']/c1);
else
        printf("%.2lf%%\n",100.0*data2[check-'A']/c1);
}
else
{
if(check>='A' && check<='Z')
check = 'a'+check-'A';
printf("%.2lf%%\n",100.0*(data1[check-'a']+data2[check-'a'])/c1);
}
}
return 0;
}

本回答被网友采纳
第3个回答  2011-06-15

我只有《C++语言程序设计》作者 郑莉 / 董渊
书本上的源代码。 要的话,留下邮箱地址 发给你 还是到书店去买本书吧!!比较好的书追问

很急的啊,现在看书来不及了,马上要交搞了!

第4个回答  2011-06-14
用字典树记录每个单词的出现个数,每次输入就更新一个节点(节点用结构体单搞一个数据结构,每个单词的词尾的节点次数加1),最后扫描所有节点,节点数=单词个数,把次数除以单词个数输出。
相似回答