Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The problem

Implement a function that replaces each space in the string with “%20”. For example, enter Weare happy., the output is We%20are%20happy.

Suppose you make a substitution on the original string, and there is enough free memory behind the string.

Your own train of thought

When I saw this problem, the first thing that came to my mind was to create two indexes, one to iterate over the valid string and one to the end of the string

When a space is encountered, %20 is placed at the end of the string

Then determine if there’s any data at the end of the string,

If not empty, these trailing characters are swapped with the characters in the valid string in turn based on the traversal index

Follow this principle until you reach the end of a valid string

Problems arising

Data interaction is required each time a character at the end of a string is placed in a valid string

And multiple indexes are logged at a time, one that traverses the valid string and one that records the character to be moved at the end of the string

There are a number of cases to consider when iterating through a valid string

Better idea

Traverse the string from back to front and move it

We start by iterating through a string, counting characters and Spaces. By replacing the space ‘ ‘with %20, we change the string from a string to a string, so the length of the string is +2 for each character

First, define two indexes, srcEndIdx pointing to the end of the original string and dstEndIdx pointing to the end of the replaced string

When srcEndIdx encounters no Spaces, place the iterated character in the dstEndIdex position, and move srcEndIdx forward by 1 space, and dstEndIdx forward by 1 space

When srcEndIdx encounters whitespace, move srcEndIdx forward 1 space and dstEndIdx forward 3 Spaces to put 02% in turn into the string

The entire string is iterated until both indexes point to the same location

code

Private static void replaceSpace (char string []) {/ / array is correct judgment the if (string = = null | | string. The length = = 0) {return; } system.out. println(" array total length: "+ string.length); Int space = 0; int charCount = 0; int i = 0; while (string[i] ! = '\0') { charCount++; if (string[i] == ' ') { space++; } i++; Int resStrLen = charCount + space * 2; / / if the number of Spaces to 0 or replacing Spaces after the total length is greater than the length of the array, then don't go the following process if (space = = 0 | | resStrLen > string. The length) {return; System.out.println(" length before replacing Spaces: "+ charCount); System.out.println(" replace space with length: "+ resStrLen); Int srcEndIdx = charcount-1; Int dstEndIdx = resstrlen-1; int dstEndIdx = resstrlen-1; // When two indexes are equal, While (dstEndIdx > srcEndIdx && srcEndIdx >= 0) {// If (string[srcEndIdx]! = ' ') { System.out.print(string[srcEndIdx] + " "); string[dstEndIdx--] = string[srcEndIdx]; } else {// If it is a space, write the corresponding %20 three characters string[dstEndIdx--] = '0'; string[dstEndIdx--] = '2'; string[dstEndIdx--] = '%'; } srcEndIdx--; } System.out.println(); // Replace the character array system.out.println (string); }Copy the code

conclusion

To learn to develop their own thinking ability, do not stick to one case, to learn to think from many aspects and perspectives. This article gives us a clearer idea of strings. When we encounter problems, we must learn to divergent their thinking, do not hit the south wall do not look back, the order is not good, we can reverse order, there is always a better way to solve the problem!