线性表存储单词并统计出现次数

//用线性表(数组或链表)存储单词,以实现单词出现次数统计。
#include
#include
#include
#define MaxSize 20
typedef struct LNode
{
  char ch[MaxSize]; //保存单词,最长单词20个字母
  int num; //保存出现次数
}Record;
//单词重复检测
bool DupliIf (char a[],Record rec[],int num)
{
  int i;
  for (i=0; i
  {
    if (!strcmp(a,rec[i].ch))//相同返回0
    {
      rec[i].num++;
      return false;
         
  }
  return true;
}
//单词处理
int WordDeal (char ch[],Record rec[])
{
  int i,j=0;
  int sum=0;
  char save[MaxSize];
  for (i=0; i
  {
    if (ch[i]>='a'&&ch[i]<='z' || ch[i]>='A'&&ch[i]<='Z')//如果是字母,进行记录
    {
      save[j] = ch[i];//保存单词
      j++;
    }
    else //读取到非字母时,处理已存储的字符
    {
      if (j!=0)
      {
        save[j] = '\0';
        if (DupliIf (save,rec,sum))//判断存不存在
        {
          strcpy (rec[sum].ch,save); 
          rec[sum].num=1;
          sum++;
        }
        //else printf ("存在相同\n");         
           
      j=0;
    }
  }
  return sum;
}
void DisRec (Record rec[],int sum)

  int i;
  for (i=0; i
  {
    printf ("%s*%d\n",rec[i].ch,rec[i].num);
  }
}
int main ()
{
  Record rec[100]; //记录26个字母
  char ch[400] = "Informally, an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output.";
  int sum;
  sum = WordDeal (ch,rec);
  DisRec (rec,sum);
  return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值