This article is participating in the “Java Theme Month – Java Brush questions punch card”, see the activity link for details

1. Title Description

Implement a function that replaces each space in a string with “%20”. For example, if the string is “We Are Happy.”, the replacement string is “We%20Are%20Happy”.

Second, train of thought analysis

Java has a function called replace() that can be used directly, but that’s not really interesting. 🤣 🤣 🤣

public String replaceSpace(StringBuffer str) {
    String result = str.toString().replace(""."% 20");
    return result;
}
Copy the code

As a result, I decided to implement it myself and not call the API directly.

  • 1. Convert the string into an array of characters, iterate over it once, and count the number of Spaces.
  • 2. Create a new character array. The initial size = the original character array length + the space length x2
  • 3. Iterate once and copy. If the value is not a space, copy directly% 20These three characters are copied over.

AC code

public class Solution {
    public String replaceSpace(StringBuffer str) {
        // Convert to a character array
        char[] originChars= str.toString().toCharArray();
        int spaceNum = 0;
        // Count the number of Spaces
        for (int i = 0; i < originChars.length; i++) {
            if (originChars[i] == ' ') { spaceNum++; }}// The length of the new character array
        int newCharsLength = originChars.length + 2 * spaceNum;
        char[] newChars = new char[newCharsLength];
        int newStrIndex = 0;
        for (int index= 0; index<=originChars.length - 1; index++) {
            if(originChars[index] ! =' ') {// Copy directly
                newChars[newStrIndex++] = originChars[index];
            }else {
                // Spaces require three characters to be copied
                newChars[newStrIndex++] = The '%';
                newChars[newStrIndex++] = '2';
                newChars[newStrIndex++] = '0'; }}// To a string
        return newString(newChars); }}Copy the code

C + + implementation

C++ implementation is similar, mainly when copying from the back to the front, so you can save some space, directly on the original array operation. There is no need to recreate a space.

class Solution {
public:
    	void replaceSpace(char *str,int length) {
		if(str == NULL || length <= 0) {return;
        }
        int originalLength = 0;
        int spaceNum = 0;
        int i;
        while(str[i++] ! ='\ 0'){
            ++originalLength;
            if(str[i] == ' '){ ++spaceNum; }}int newCharsLength = originalLength + 2 * spaceNum;
        
        int indexOriginal = originalLength-1;
        int index = newCharsLength-1;
        
        while(indexOriginal >= 0 && index > indexOriginal){
            if(str[indexOriginal] == ' '){
                str[index--] = '0';
                str[index--] = '2';
                str[index--] = The '%';
            }
            else{ str[index--] = str[indexOriginal]; } --indexOriginal; }}};Copy the code

conclusion

There may be some apis that do a lot of great things, but it’s important to know why. It’s nice to do it yourself once in a while.