第十六周项目二 统计单词的个数

在第十六周的项目中,通过编程实现了统计文本中单词个数的功能。运行结果显示操作成功,学习过程中了解到,使用特殊字符' '可以有效区分单词。
摘要由CSDN通过智能技术生成
/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:刘佳琦
 * 完成日期:2014年 12 月 14 日
 * 版 本 号:v1.0
 *
 * 问题描述:统计并输出单词的个数。
 * 输入描述:无
 * 程序输出: 单词的个数。
 */
#include <iostream>

using namespace std;
int pwordnum(char *str);
int main()
{
    char str[50]= {"I am a student"};
    cout<<pwordnum(str);
    return 0;
}
int pwordnum(char *str)
{
    int i,num=0;
   for(i=0;str[i]!='\0';i++)
   {
       if(str[i]!=' '&&str[i+1]==' ')
        num++;
   }
   if(num==0)
   num=-1;
   num=num+1;
    return num;
}


运行结果:

学习心得:字符用‘’就可。

(1)编GtTt)现算文“testtxt”该文本文件中的内容如下所示: Good now, sit down, and tell me, he that knows. Why this same strict and most observant watch So nightly toils the subject of the land, And why such daily cast of brazen cannon, And foreign mart for implements of war, Why such impress of shipwrights, whose sore task Does not divide the Sunday from the week; 第5部分编程测试题 81 What might be toward, that this sweaty haste Doth make the night joint-labourer with the day: Who is't that can inform me? (2)将读取的文本进行分词后放入列表lw中(注意处理大小写和除标点符)并按厅16个单词的格式输出结果,如图 5-2 所示。 调信的结果为: sod nou sit domm and tell me he that knows why this sime strict and mast lstrvant watch so nightlz toils the subiect of the land and sto maoh aily caat of nzen cannen and foreign micrt for implements of war why ruch iwprest of shipnrigits whecs shr it does not divide the sunday from the weok what might be torand that this iweety doth make the nlght oint-lab urer viih the day wno itt that ca inlers me 图5-2输出结果1 (3)根据列表lw的内容生成字典dic(键为单词,值为单词个数)。 (4)根据字典dic,分别输出单词of、and、such、why的个数,按图 5-3所示的格式输出结果。 列表 海结更为: 买,有4个 and有4个 auch有2个 he有3个 图5-3输出结果2 (5)将写程文件名“zhpy”保存算M盘根目录。 请写出代码并说明代码的意思
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值