用户从控制台输入文章、计算文章中单词个数并存入文件中


/*
 用户从控制台输入一篇文章,输入完成以后,判读文章中一共有多少个单词?
 char txt[2000];
 
 hello lili,hello hameimei,nice to meet you.
 hello nice to meet you too.

 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define  N  2000




void mystrcat(char * str1,char * str2)
{
    while (*str1!='\0') {
        str1++;
    }
    while (*str2!='\0') {
        *str1=*str2;
        str2++;
        str1++;
    }
    *str1='\0';
}


int main(int argc, const char * argv[])
{
    char txt[N];
    
    //提示用户输入文章
    printf("请输入你的文章:");
    
    gets(txt);
    //printf("%s\n",txt);
    for (int j = 0; txt[j]!= '\0'; j++) {
        putchar(txt[j]);
    }
    
    printf("\n");
    
    int num = 0;//单词个数
    int i = 0;
    while (txt[i] != '\0')
    {
        if (((txt[i]>=48&&txt[i]<=57)||(txt[i]>=65&&txt[i]<=90)||(txt[i]>=97&&txt[i]<=122))&&((txt[i+1]<48)||(txt[i+1]>57&&txt[i+1]<65)||(txt[i+1]>90&&txt[i+1]<97)||(txt[i+1]>122)))
        {
            num++;
        }
        i++;
    }
    
    printf("文章单词个数为%d\n",num);


    // 文件处理
    FILE *fp;
    char fileName[20];
    printf("请输入文件名:");
    scanf("%s",fileName);
    
    char newname[] = {"//Users//haixia//Desktop//"};
    mystrcat(newname, fileName);
    
    //文件打开
    fp = fopen("//Users//haixia//Desktop//新建文件.txt", "w");
    rename("//Users//haixia//Desktop//新建文件.txt",newname);
    //写入文件
    fprintf(fp, txt);
    //关闭文件
    fclose(fp);
  
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值