“This is the 28th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Today we’re going to flip the word in the string

Implementation approach

We can use PHP’s built-in functions directly to manipulate strings and return the flipped result.

One or more Spaces occur at the beginning or end of a string or between words.

  • We can remove the first and last Spaces, then make the middle space of each word one, then put each word in the array, and finally flip the array to print each element.
  • We can also simply iterate through the string, recording each word into the array, and finally flipping the array to output each element.

The above two methods use built-in PHP functions or iterate over the string multiple times. Here’s a way to iterate through the string once to get the result.

First, we iterate through the whole string in reverse. No matter how many Spaces there are between two words, as long as the current position is a space and the position before the current position is not a space, we have reached the edge of a word and need to extract the word.

Because you just flip the order of the words in the string, not every letter of the word, you need to store each character of the iterated string into a variable first, and when you reach the edge of the word, put the entire word into the result.

  • When a non-space is encountered, the current character is put into a variable.
  • When a space is encountered, determine whether the character preceding the current position is a space. If it is not a space, it is on the edge of a word, and place the temporary word in the result.

The above is a general idea, there are some first and last Spaces to deal with the problem, the following code details.

The complete code

Line 725 gets the length of the string, assuming that the real string has no Chinese characters.

Line 726, define a variable, the flipped result string.

Line 727 defines a variable that temporarily holds a single word.

Lines 728-746 traverse the entire string.

In line 729-736, if the current character is not a space, put the current character into the variable $currentWord, which temporarily holds the word. Since the string is traversed backwards, we need to concatenate the current character with $currentWord each time.

If the current traversal position is 0, it is the first character position of the string. That is, when the first position of the string is not a space, the word must be concatenated with the inverted string $execWords because there are no other space characters to judge as the boundary of the entire word.

In line 737-745, the current character is a space, and the last character ($I +1) is not a space, which is the boundary position of a word, indicating that the variable containing the word is a complete word. If the previous position is also a space, it is a continuous space.

Why is that judgment made?

$i < $len - 1
Copy the code

If the character in the first position is a space, there is no previous position, so you need to start from the second position.

If the above condition is met, concatenate the currently stored word with the entire flipped string $execWords and empty the stored word variable $currentWord to record a word.

Line 748 returns the flipped string.

Lines 732-734 and 739-741 are not specified. This judgment is used to deal with the problem of beginning and end Spaces when the flipped string needs to concatenate a new word, either through

$execWords. =' ' . $currentWord;
Copy the code

Or through

$execWords. =$currentWord . ' ';
Copy the code

Will result in a space before the first word or after the last word. With this judgment, you can avoid this emptying.

Alternatively, we can remove these two judgments and use the following method to get the full flipped string.

$execWords = trim($execWords);
Copy the code

You just use PHP’s built-in functions.

Here is the result of executing on leetCode, and it feels good.