第二周代码(wc项目)

     这次的项目来源于网站https://www.dssz.com/2304637.html  

用C语言实现wc命令,统计文件的行数,字符数,词组数。楼主发的代码似乎并不是完整代码,但这些基本就是wc项目需求的。

 

      首先是添加单词,下面是代码://先记录单词,在运用指针指向他

void addWord(char *w1)        //添加单词
{

    link *p1,*p2;
    //if(w1[0] <= 'Z' && w1[0] >= 'A')    //转成小写字母
    //{
    //    w1[0]+=32;
    //}
    for(p1=head;p1!=NULL;p1=p1->next)    //判断单词在连表中是否存在
    {
        if(!strcmp(p1->w,w1))
        {
            p1->count++;   //存在就个数加1
            return;
        }
    }

    p1=(struct word *)malloc(sizeof(word));//不存在添加新单词
    strcpy(p1->w,w1);
    p1->count=1;
    p1->next=NULL;
    count++;    //总的单词数加加

    if(head==NULL)
    {
        head=p1;
    }
    else
    {
        for(p2=head;p2->next!=NULL;p2=p2->next);
        p2->next=p1;
    }
}

然后判断指向的是是不是字符或是其他
int isnotWord(char a)        //判断是否为字母
{
    if(a <= 'z' && a >= 'a')
    {
        littleletter++;
        return 0;
    }
    else if(a <= 'Z' && a >= 'A')
    {
        bigletter++;
        return 0;
    }
    else if(a==' ')
    {
        space++;
        return 1;
    }
    else if(a>='1'&&a<='9')
    {
        number++;
        return 1;
    }
    else if(a=='\n')
    {
        hang_num++;
        return 1;
    }
    else
    {
        other++;
        return 1;
    }

}
统计单词个数:
#include <iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
//统计单词的个数
int main()
{
    char a;
    int count=0;
    while((a=getchar())!='\n')
    {
        if(a==' ')
             count++;
    }
    cout << count+1 << endl;
    return 0;
}

转载于:https://www.cnblogs.com/tgq0101457/p/7597858.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值