改写要求1:将字符数组str改为字符指针p,动态开辟存储空间

改写要求2:增加统计关键字个数的函数void CountKeyWords()

改写要求3: 增加替换函数void FindKeyWords()

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std; class WORDNUM
{
char *p;
double c;
public:
WORDNUM(char *s)
{
p=new char[strlen(s)];
strcpy(p,s);
c=;
}
void process();
void CountKeyWords(string key[],int len);
void FindKeyWords(string key[],string swapkey[],int len);
void print()
{
char* r=new char[strlen(p)];
strcpy(r,p);
while(*r!='\0')
cout<<*(r++);
cout<<endl;
cout<<"num="<<c<<endl;
}
}; void WORDNUM::CountKeyWords(string key[],int len)
{
int i=len;
while(i)
{
double sameword=;
char* r=new char[strlen(p)];
strcpy(r,p);
char* q=new char[key[i-].length()];
strcpy(q,key[i-].c_str());
char* find=strstr(r,q);
while(find)
{
memset(find, ' ', strlen(q));
sameword++;
find=strstr(find,q); } cout<<key[i-]<<"的个数为:"<<sameword<<"占全部字符"<<(sameword/c)*<<"%"<<endl;
i--;
}
} void WORDNUM::FindKeyWords(string key[],string swapkey[],int len)
{
int i=len;
string temp;
temp=p;
int pos=temp.find(key[i-]);
while(i)
{
while(pos!=-)
{
temp.replace(pos,key[i-].length(),swapkey[i-]);
pos=temp.find(key[i-]); }
i--;
}
memset(p,,sizeof(p));
strcpy(p,temp.c_str());
} void WORDNUM::process()
{
int word=;
int len;
len=strlen(p);
char* r=new char[strlen(p)];
strcpy(r,p);
for(int i=;i<len;i++)
{
if(((r[i]>='a'&&r[i]<='z')||(r[i]>='A'&&r[i]<='Z'))&&word)
{
c++;
word=;
}
else if(r[i]==' ')
word=;
}
} int main(int argc, char *argv[])
{
string key[]={"nice","girl"};
string swapkey[]={"ugly","boy"};
int len=sizeof(key)/sizeof(key[]);
string str="She is a nice nice girl girl girl hi";
char* split=new char[strlen(str.c_str())];
strcpy(split,str.c_str());
WORDNUM w(split);
w.process();
w.CountKeyWords(key,len);
w.print();
w.FindKeyWords(key,swapkey,len);
w.print();
system("PAUSE");
return EXIT_SUCCESS;
}

C++程序设计实践指导1.9统计与替换字符串中的关键字改写要求实现的更多相关文章

  1. C++程序设计实践指导1.8求指定范围内的所有素数改写要求实现

    改写要求1:以指针为数据结构动态开辟存储空间 #include <cstdlib> #include <iostream> using namespace std; class ...

  2. 使用PHP的strstr()函数来统计一段字符串中元音字母的个数(区分大小写)

    <?php/**练习:统计一段字符串中所有元音字母的个数(区分大小写)*/$str='This is a test file.'; //原始字符串echo $str.'<br>'; ...

  3. 统计字符在字符串中第n次出现的位置

    输入一个字符串s,一个数字n和一个字符c,统计这个字符c在字符串s中第n次出现的位置 输入格式: 输入3行.第1行是字符串s,第2行是数字n,第3行是被查找的字符c. 输出格式: 第n个字符在字符串中 ...

  4. C++程序设计实践指导1.15找出回文数改写要求实现

    改写要求1:用单链表实现 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode ...

  5. C++程序设计实践指导1.14字符串交叉插入改写要求实现

    改写要求:1:以指针为数据结构开辟存储空间 改写要求2:被插入字符串和插入字符串不等长,设计程序间隔插入 如被插入字符串长度为12,待插入字符串长度为5 则插入间隔为2 改写要求3:添加函数Inser ...

  6. C++程序设计实践指导1.13自然数集中找合数改写要求实现

    改写要求1:用单链表实现 改写要求2:析构函数中依次将链表结点删除 #include <cstdlib> #include <iostream> using namespace ...

  7. C++程序设计实践指导1.12数组中数据线性变换改写要求实现

    改写要求1:分别用指针pa.pb代替数组 改写要求2:从键盘输入data元素 元素个数任意,输入0结束 #include <cstdlib> #include <iostream&g ...

  8. C++程序设计实践指导1.10二维数组元素换位改写要求实现

    改写要求1:改写为以单链表和双向链表存储二维数组 改写要求2:添加函数SingleLinkProcess()实现互换单链表中最大结点和头结点位置,最小结点和尾结点位置 改写要求3:添加函数Double ...

  9. C++程序设计实践指导1.1删除序列中相同的数改写要求实现

    改写要求1:改写为以指针为数据结构 #include <iostream> #include <cstdlib> using namespace std; class ARP ...

随机推荐

  1. C++ 语言特性的性能分析

    转载:http://www.cnblogs.com/rollenholt/archive/2012/05/07/2487244.html      大多数开发人员通常都有这个观点,即汇编语言和 C 语 ...

  2. 【译】JavaScript 开发者年度调查报告

    截至目前有超过了 5000 人参与了(该次调查),准确的说是 5350 人.我迫不及待的想要和大家分享一下这次调查的细节.在分享之前我想要感谢参与调查的每一个人.这是 JavaScript 社区一个伟 ...

  3. WSGI的理解

    Python web开发中,服务端程序可分为2个部分: 服务器程序(用来接收.整理客户端发送的请求) 应用程序(处理服务器程序传递过来的请求) 在开发应用程序的时候,我们会把常用的功能封装起来,成为各 ...

  4. 2013年9月份阿里JAVA面试经历

    面试时间:2013-9 面试地点:合工大 面试内容: 1. struts2怎么实现的,原理是什么 2. session是怎么实现的?存储在哪里? 3. Java怎么创建链表的? 定义一个结点类,再定义 ...

  5. Qt之HTTPS登录(集成QNetworkAccessManager提前修改QSslConfiguration,然后post)

    简述 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP ...

  6. 关于在Reshaper中添加代码模板代码段

    http://www.cnblogs.com/tristinjet/archive/2009/08/19/1550203.html 去 tools->模板中进行模板编辑设置

  7. grep搜索当前目录和递归搜索子目录中文本文件的特定pattern

    一般在windows上文本编辑器notepad++,UE这些都有这些功能,Linux下就换了一种方式,用grep来完成文件中信息查找的方式. grep -R -n --include="*. ...

  8. HDOJ-1009 FatMouse' Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=1009 # include <stdio.h> # include <algorithm> ...

  9. Add Digits 解答

    Question Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  10. openssl CA 自签证书,阿里云配置tomcat https

    <一,openssl CA自签发证书> 1,生成私钥 openssl genrsa 1024 > private.key;