改写要求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#遍历数组

    Eg: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  2. struts1:(Struts)ActionForm类及表单数据验证

    在Struts的中央控制器中写了Struts的控制器角色,在这篇介绍下Struts的视图!Struts的视图组件:Struts框架中的视图组件主要包括:JSP页面.ActionForm类.Struts ...

  3. OpenCV——像素数据类型总结<摘>

    1.Unsigned 8bits(一般的图像文件格式使用的大小)IplImage数据结构参数:IPL_DEPTH_8UCvMat数据结构参数:CV_8UC1,CV_8UC2,CV_8UC3,CV_8U ...

  4. (转) class II

    Overloading operators   Classes, essentially, define new types to be used in C++ code. And types in ...

  5. [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud     Going from u to v or from v to u? Tim ...

  6. im2uint8函数分析

    环境:Win7 64位 + Matlab R2010a 本次分析的函数为im2uint8,这个函数在图像处理中要用到,主要把图像数据类转换到uint8 uint8函数有效的输入的图像数据类为:logi ...

  7. 一大坨GoogleAPI的学习笔记之一(oAuth2.0)

    官方文档地址:https://developers.google.com/accounts/docs/OAuth2InstalledApp 最近Ubuntu下VGA接口无端的不能用了,木有心情翻译了, ...

  8. 飘逸的python - 多条件排序及itemgetter的应用

    曾经客户端的同事用as写一大堆代码来排序,在得知python排序往往只需要一行,惊讶无比,遂对python产生浓厚的兴趣. 之前在做足球的积分榜的时候需要用到多条件排序,如果积分相同,则按净胜球,再相 ...

  9. javascript中写不写$(function() {});的区别

    原地址 $(document).ready() 里的代码是在页面内容都加载完才执行的,如果把代码直接写到script标签里,当页面加载完这个script标签就会执行里边的代码了,此时如果你标签里执行的 ...

  10. Scrapy学习系列(一):网页元素查询CSS Selector和XPath Selector

    这篇文章主要介绍创建一个简单的spider,顺便介绍一下对网页元素的选取方式(css selector, xpath selector). 第一步:创建spider工程 打开命令行运行以下命令: sc ...