Home > Back-end >  Why is this so
Why is this so

Time:09-17

# define _CRT_SECURE_NO_WARNINGS//close security
#include
#include
#include

Void tihu (char * * PPSTR, char * old, char * new)
{
Int pplen=strlen (* PPSTR);//the original length of the string
Int olen=strlen (old); The length of the//replaced
Int nlen=strlen (new); The length of the//replace

Char * p1=* PPSTR;//create two Pointers to save the original string address
Char * p2=* PPSTR;

While (* p1!='\ 0')//cycle
{
* p1=* (p2);//2 dislocation pointer

Int flag=1;//assume that find 1

for (int i=0; I & lt; Olen. I++)
{
If (p2 [I]!=old [I] | | p2 [I]=='\ 0')//looking for
{
Flag=0;//to find
break;//jump out
}
}

If (flag==0)
{
P1 + +;
The p2 + +;
}
The else
{
Int n=p1 - * PPSTR;//confirm position
Int m=p2 - * PPSTR;

(* * PPSTR=_recalloc PPSTR, pplen + nlen olen + 1, 1);//expanded memory + 1 '\ 0'

P1=* PPSTR + n.//retrieve location
The p2=* PPSTR + m;

For (char * p=p2 + strlen (p2); P & gt;=(p2); P -)//mobile string
{
* (p + nlen olen)=* p;
}

for (int i=0; I & lt; Nlen; I++)//copy
{
The p2 [I]=new [I];
}

}



}

}

Void main ()
{
Int slen=strlen (" aaaaaaaa like aaaaa like aaaaaaaa ");//the original length of the string
Char * p=malloc (slen + 1);//allocate memory space

Strcpy (p, "aaaaaaaa CCCC aaaaa CCCC aaaaaaaa");//copy the string
Tihu (& amp; CCCC p, ""," BBBB BBBB BBBB BBBB ");//replace


Printf (" % s ", p);


system("pause");
}

  • Related