改写要求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. IT学习方法

    IT 技术的发展日新月异,新技术层出不穷,具有良好的学习能力,能及时获取新知识.随时补充和丰富自己,已成为程序员职业发展的核心竞争力.本文中,作者结合多年的学习经验总结出了提高程序员学习能力的三个要点 ...

  2. Wordpress更换主题之后出错

    今天吃完午饭,休息休息,最近搞了一下google adsense,不过最终的审核没通过,我想会不会是界面不好看呢,饭后就在电脑旁,更换了几个wordpress主题,我的博客使用wordpress搭建的 ...

  3. jquery find()和filter()的区别

    在写js查找当前标签下某一子元素的子元素集合时,用到了此方法 $(".flyout-trigger").mouseover(function(){ var maxnum=$(thi ...

  4. js控制浏览器后退

    <script type="text/javascript"> window.history.forward(1); </script>

  5. MVC 中使用uploadify上传图片遇到的蛋疼问题

    MVC 中使用uploadify上传图片遇到的蛋疼问题 初次使用uploadify上传图片,遇到了一些比较纠结的问题,在这里和大家分享下,有不对的地方还望大神多多指教,希望对刚接触的朋友有所帮助,文采 ...

  6. information_schema.column_privileges 学习

    mysql 的授权是分层次的 实例级 | 库级 | 表级 | 列级 而这些授权信息被保存在了mysql.user | mysql.db | mysql.tables_priv | mysql.colu ...

  7. SQL Server 索引重建的 4 种方法

    解决方法 方法 1. 重建指定索引,这种方法没有性能可谈.重建时表还不可访问. 方法 2. 在线重建索引,只有SQL Server 企业版才支持. 方法 3. 使用填充因子重建,这样做不一定可以减小查 ...

  8. html 标签释义

    position  位置  给....定位   作用:定位 position:fixed  锁定游览器位置 如右下角弹窗 absolute  绝对定位    游览器左上角      position: ...

  9. js 创建对象

    1.工厂模式 function createPerson(name, age, job) { var o = new Object(); o.name = name; o.age = age; o.j ...

  10. C语言经典算法100例

    [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. ...