C + + Programming Practice Guide 1.9 statistics and substitution of keyword rewriting requirements in a string implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.