Rewrite requirement 1: Change the character array str to the character pointer p to dynamically open up storage space
Rewrite requirement 2: Increase the number of statistics keywords function void countkeywords ()
Rewrite requirement 3: Add replacement function void Findkeywords ()
#include <cstdlib>#include<iostream>#include<string>using namespacestd;classwordnum{Char*p; DoubleC; Public: Wordnum (Char*s) {p=New Char[Strlen (s)]; strcpy (P,s); C=0; } voidprocess (); voidCountkeywords (stringKey[],intLen); voidFindkeywords (stringKey[],stringSwapkey[],intLen); voidprint () {Char* r=New Char[Strlen (P)]; strcpy (R,P); while(*r!=' /') cout<<* (r++); cout<<Endl; cout<<"num="<<c<<Endl; }};voidWordnum::countkeywords (stringKey[],intLen) { intI=Len; while(i) {Doublesameword=0; Char* r=New Char[Strlen (P)]; strcpy (R,P); Char* q=New Char[key[i-1].length ()]; strcpy (Q,key[i-1].c_str ()); Char* find=strstr (R,Q); while(Find) {memset (Find,' ', strlen (q)); Sameword++; Find=strstr (FIND,Q); } cout<<key[i-1]<<"the number is:"<<sameword<<"Total characters"<< (SAMEWORD/C) * -<<"%"<<Endl; I--; }}voidWordnum::findkeywords (stringKey[],stringSwapkey[],intLen) { intI=Len; stringtemp; Temp=p; intPos=temp.find (key[i-1]); while(i) { while(pos!=-1) {temp.replace (pos,key[i-1].length (), swapkey[i-1]); POS=temp.find (key[i-1]); } I--; } memset (P,0,sizeof(p)); strcpy (P,temp.c_str ());}voidwordnum::p rocess () {intword=1; intLen; Len=strlen (P); Char* r=New Char[Strlen (P)]; strcpy (R,P); for(intI=0; i<len;i++) { if(((r[i]>='a'&&r[i]<='Z')|| (r[i]>='A'&&r[i]<='Z')) &&word) {C++; Word=0; } Else if(r[i]==' ') Word=1; }}intMainintargcChar*argv[]) { stringkey[2]={" Nice","Girl"}; stringswapkey[2]={"Ugly"," Boy"}; intlen=sizeof(key)/sizeof(key[0]); stringStr="She's 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"); returnexit_success;}
C + + Programming Practice Guide 1.9 statistics and substitution of keyword rewriting requirements in a string implementation