In network programming, the special characters contained in the URL parameter need to be replaced with "%20" by the two-bit hexadecimal method of the ASCII code, which is converted to a character that the server can recognize, such as a space ASCII code of 32, or 16 binary 0x20.
Title: Implement a function that replaces each space in the incoming char* string with "%20", such as "We are happy.", then output "we%20are%20happy."
The solution idea of time complexity is O (n):
First, the string is traversed, the length of the string is originallength and the number of space Numberofblank is counted, and the replacement string length newlength = Originallength+2*numberofblank;
Copy and move forward from the end of the string, using two subscript indexoforiginal and Indexofnew to mark the current position of the original character and the position of the substituted character;
When indexoforiginal points to a space, indexofnew the character of the subscript and its forward 1, 2 positions are replaced with ' 0 ', ' 2 ', '% ', and after the substitution is complete indexoforiginal move forward one cell. The indexofnew moves forward 3 cells, otherwise they move one cell forward;
Finally, add ' newLength-1 ' at the end of the string, which is labeled as the trailing point.
Code:
voidReplaceblank (CharStr[],intmaxLength) { if(str = = NULL | | maxLength <=0) return; //calculate the new length after replacing and number of Blanks intOriginallength =0; intNumberofblank =0; intindex =0; for(index =0; Str[index]! =' /'; index++) { if(Str[index] = =' ') ++Numberofblank; } originallength= index+1; //If the number of blank is 0, nothing changes if(numberofblank==0) return; //if the new length exceeds the max length intNewlength = originallength+2*Numberofblank; if(Newlength >maxLength) {cout<<"new Length exceeds the Max length!"<<Endl; return; } //start at the character before ' intIndexoforiginal = originallength-2; intIndexofnew = newlength-2; while(indexoforiginal>=0&& indexofnew >indexoforiginal) { if(Str[indexoforiginal] = =' ') {str[indexofnew--] ='0'; Str[indexofnew--] ='2'; Str[indexofnew--] ='%'; } Else{str[indexofnew--] =Str[indexoforiginal]; } --indexoforiginal; } //Add a ' + ' at the laststr[newlength-1] =' /';}
Full code:
#include <iostream>#include<stdio.h>using namespacestd;voidReplaceblank (CharStr[],intmaxLength) { if(str = = NULL | | maxLength <=0) return; //calculate the new length after replacing and number of Blanks intOriginallength =0; intNumberofblank =0; intindex =0; for(index =0; Str[index]! =' /'; index++) { if(Str[index] = =' ') ++Numberofblank; } originallength= index+1; //If the number of blank is 0, nothing changes if(numberofblank==0) return; //if the new length exceeds the max length intNewlength = originallength+2*Numberofblank; if(Newlength >maxLength) {cout<<"new Length exceeds the Max length!"<<Endl; return; } //start at the character before ' intIndexoforiginal = originallength-2; intIndexofnew = newlength-2; while(indexoforiginal>=0&& indexofnew >indexoforiginal) { if(Str[indexoforiginal] = =' ') {str[indexofnew--] ='0'; Str[indexofnew--] ='2'; Str[indexofnew--] ='%'; } Else{str[indexofnew--] =Str[indexoforiginal]; } --indexoforiginal; } //Add a ' + ' at the laststr[newlength-1] =' /';}voidTestCharStr[],intmaxLength) {cout<<"-----------Test-------------"<<Endl; printf ("Original:%s\n", str); Replaceblank (str, maxLength); printf ("Replaced:%s\n", str); cout<<"-----------End---------------"<<Endl; }intMain () {/*char str[50] = "Hello World and S"; Replaceblank (str, 50); printf ("%s\n", str); */ Charstr1[ -]="Hello World and S"; Charstr2[ -]=""; Charstr3[ -]="Hello world! fdsjkd"; Charstr4[ -]="HELLOWERODDFSD"; Charstr5[ -]=" "; Test (STR1, -); Test (STR2, -); Test (NULL, -); Test (STR3, -); Test (STR4, -); Test (STR5, -); return 0;}
Reference: "Sword refers to the name of the Enterprise interview officer explaining typical programming problem"
C + + string space substitution problem