String of data structures and algorithms

String is a sequence of several characters, C / C ++ and each string with '\ 0' as the end, so it is easy to find the last end of the string, but requires an additional overhead of character, likely to cause a string the cross-border.

Constant string in a separate read-only memory area, whose contents can not be changed;

 

Alternatively a space (the offer --- wins the interview questions 4)

Title: Please implement a function, each of the spaces in the replacement string as "20%." For example, enter "We are happy.", The output "We% 20are% 20apy.".

  • void ReplaceBlank(char string[], int length);

Problem-solving ideas:

(1) traverse the string, the statistics of the total number of numberOfBank spaces;

(2) calculate the length of the string after the replacement, i.e. newLength = originLength + 2 * numberOfBank;

(3) two pointers P1 and P2 point to the end of the string after the end of the string and replace the original, and the copy traversed sequentially from the front;

2 To be continued ~

Guess you like

Origin www.cnblogs.com/bo1990/p/11441983.html