I. Description of requirements
Enter a string that the program compiles and outputs from each word in the string. For example, if the input string is "Hello, how does you do", then the output string is "Do what Hello,". Note keep the spaces between each word and the corresponding punctuation.
Second, the algorithm design
By observing the sample string (that is, "Hello, How Does"), we can see that the relationship between each word and the space in the string is: total number of words = total number of spaces +1. That is, the total number of spaces in the sample string is 4, and the number of words is 5 (that is, "Hello," "How", "Does", "You", and "do").
Therefore, we can consider finding the total number of spaces in the input string, then finding each word according to the space and then assembling the words in reverse order.
The overall process of the program is shown in 1.
Figure 1 The overall process of the program
III. Special Process Considerations
In the process of writing the program, we have to consider the format of the input string, such as:
1. Enter the first few characters of the string is a space, that is, like "hello,how do", we need to start with a few spaces to remove, and then the subsequent processing.
2. Enter the string at the end of a few characters is a space, that is, like "Hello, How does do", we need to first remove the end of a few spaces, and then the subsequent processing.
3. The characters in the middle of the input string are contiguous spaces, i.e., "Hello, How do youdo", we need to combine contiguous spaces into a single space before processing.
4. As long as there is a space between the two words in the input string, we treat them as two different words, even if the two words have no practical meaning, that is, "Hello, how does y ou do", where the "Y" and "ou" are combined to make sense, But we are still dealing with two different words.
Iv. Code of the program
Five, the procedure test
we will compile Write the program "REVERSETHESTRING.C" upload to the Linux machine, and use "Gcc-g-o reversethestringreversethestring.c" command to compile the program, generate " Reversethestring "file. The program is tested in detail below.
1. When the input string is "Hello, How Does", the program runs as follows:
Please input the string:
Hello, How does
Teststr=hello.
Resultstr=do how Hello,
2. When the input string is "Hello, How does" (Note that there are two spaces in front), the program runs as follows:
Please input the string:
Hello, How does
Teststr= Hello, how doyou do
Resultstr=do how Hello,
3. When the input string is "Hello, How does" (Note that there are two spaces behind), the program runs as follows:
Please input the string:
Hello, How does
Teststr=hello.
Resultstr=do how Hello,
4. When the input string is "Hello, How does" (Note that there are contiguous spaces in the middle), the program runs as follows:
Please input the string:
Hello, How does
Teststr=hello, HOWdo
Resultstr=do how Hello,
5. The input string is "Hello, ho W do yo u Do" (Note that a few complete words are separated by a space) when the program runs as follows:
Please input the string:
Hello, ho W do yo u Do
Teststr=hello, ho W do yo u Do
Resultstr=do u yo do w ho Hello,
Can be seen, for the above considerations of the special circumstances, the program will be able to make the correct treatment.
Vi. Expansion of demand
Based on the requirements and procedures in this article, we can consider the following extensions to the requirements:
1. Convert the first letter size of the string after the reverse order, and turn the first letter of the reverse order from uppercase to lowercase, and turn the "Hello, How does do" to "do".
2. Do not change the position of the last punctuation in the original string, will be "Hello, how does you do!" Convert to "Do what hello,!".
3. Remove the punctuation from the original string, and you will be "Hello, how do do!" Convert to "Do what Hello".
"String processing algorithm" The algorithm design and C code implementation of the inverse order of each word in the input string