Anyone who has used VB knows that the replace in VB is very useful. It seems that there is no ready-made function in C ++ to implement this function (except cstring in MFC). Let's write it by ourselves.CodeImplement the function.
The C ++ implementation is as follows:
# Include <iostream>
# Include <string>
Using namespace STD;
String Replace (string a, string S, string d ){
While (A. Find (s )! = String: NPOs)
A. Replace (A. Find (s), S. Length (), d );
Return;
}
Main (){
Cout <Replace ("asdfgsdh", "SD", "1123 ");
}
C LanguageThe implementation is as follows:
# Include <iostream>
# Include <string>
Using namespace STD;
Void R (char * s, char * S1, char * S2, char * S3 ){
Char * Ss = s;
Char * P;
P = strstr (SS, S1 );
If (P = NULL ){
S3 = 0;
Return;
}
While (P! = NULL ){
Int n = p-ss;
Strncat (S3, SS, N );
Strcat (S3, S2 );
Ss = P + strlen (S1 );
P = strstr (SS, S1 );
}
Strcat (S3, SS );
}
Main (){
Char * A = "asdasasdsd ";
Char s [30] = {0 };
R (A, "ASD", "123ui", S );
Cout <s;
}
the code is relatively simple. If you have nothing to say about it, you should be a trainer.