Reverse string by word
Topic requirements: put strings "
I am A Student reverses becomes "Student A am I" without any library function.
There are many ways to reverse the word order in the string, we can define a stack structure, according to the characteristics of the stack, advanced. We can use the punctuation to distinguish between the punctuation, but use the punctuation of the punctuation, but use the punctuation, it does not mean two words.
Western world counting methods like to use triple numbers to add a comma-like form such as "3,483,123", although we can find "," but cannot use "3, 483, 123" to distinguish between three words, should be seen as a word. If we think about characters such as Chinese and English, distinguish between words will be more complicated, but fortunately, we don't have to consider such complex situations) to distinguish words, then press each word in turn, then read in turn from the stack Each word, so that the word sequence of the entire string is reversed, but we have to consider three problems during the specific implementation:
1: We want to define a stack structure to save each word, and maintain, manage, fill this stack structure during the process of reverse strings, we have to make judgments for the number of characters in each application fill words In order to save this word dynamic application space, when we use it, even
DELETE structure prevents memory leakage. This way our main energy will be placed on the maintenance of this stack, not the word order.
2: When the word analysis is analyzed, if we directly press the word directly pressed, we simplify the bids between the two words, which may cause the reversal string and the original string without match, such as "
I am A Student "A and Student are directly two spaces, then output the string after the invertent words in the above manner is" Student A am i "Student and A between only one space, this may not be very obvious, if The original string is divided by the punctuation, and we fill the space, the resulting string may differ from the true meaning of the original string. For example, "Student A, AM i" is reversed "I am A Student "instead of" I am, a student ". In the face of such a problem, you have to define a stack structure to save the relationship between the two words, or the word that is originally in the stack is not true words, put spaces Or punctuation symbols are saved.
3: Because our stack holds the reverse word data, every time the reverse data is used, the stack will be empty once, and the word must be analyzed again before use. Avoid this problem method, you can use the array meaning of the stack, that is, add an index operation in the stack. Press into the stack inverse sequence index to lead the data, but unfortunately, you don't wait to re-modify your own stack structure.
Comprehensive consideration, use the stack structure, we have to spend too much energy to consider the stack structure, and the stack maintenance during the use, delete the stack structure, prevent resource leakage. And after the stack structure is used, the algorithm is not "original" algorithm. At least double the space of the string space, in the memory tight system (such as embedding), this approach is not advisable. However, this method is not an advantage. The advantage is that the string (a paragraph) only needs to be traversed once, you can get the string after reverse.
Here we must explain another method: use the reverse string method to achieve the word order inversion. Basic Principle: First, we will reverse the string (a paragraph) to be reversed. For example, "I am a student" reverses to "TNEDUTS A MA I", then repeatedly reversible, and finally "Student A am I ". Using this method, the advantage of using a stack structure is that we use an inverting algorithm, instead of the stack structure, this algorithm is "original" algorithm, no need to apply space, as long as memory can accommodate strings, words can be implemented Inversion. The disadvantage is that you have to write a function of getting a string length and almost traversed two strings.
Here is the specific code to use this method:
/ ************************************************** ********************** /
//
Function Name: USTRLEN
//
Enter parameters: strsource, designer string;
//
Output parameters: int, the length of the string.
//
Description: Depending on the character '/ 0' to get the length of the string
/ ************************************************** ********************** /
int
USTRLEN (Const Char * STRSOURCE)
{
//
Declaration variable
INT ILENGTH (0);
//
Traversing string, find characters '/ 0'
While (* STRSOURCE ! = '/ 0')
{
ilength;
}
//
Returns the length of the string
Return Ilength;
}
/ ************************************************** ********************** /
//
Function Name:
_Reversalchar
//
Enter parameters: strsouce, to be reversed strings; iStart, rotating string start position; IEND, rotating string end position
//
Output parameters: char *, the pointer of the string after reverse;
//
Description: String between ISTART to String IEND
/ ************************************************** ********************** /
charr
* _Reversalchar (Char * STRSOUCE, INT ISTART, IEND)
{
//
Reverse string
For (; IEND> ISTART; iStart, - Iend)
{
CHAR CH;
CH = STRSOUCE [iStart];
STRSOUCE [ISTART] = STRSOUCE [IEND];
STRSOUCE [IEND] = CH;
}
//
Returns a string pointer
Return STRSOUCE;
}
/ ************************************************** ********************** /
//
Function Name: Reversalchar
//
Enter parameters: strsource, to be reversed strings
//
Output parameter: char *, pointer after reversing strings
//
Description: Press the word reverse string
/ ************************************************** ********************** /
charr
* Reversalchar (Char * STRSOUCE)
{
//
Get the length of the string
INT ILENGTH = USTRLEN (STRSOUCE);
//
Reverse the whole string
_Reversalchar (strsouce, 0, inglen-1);
//
Declare variables (the beginning of the word and the end of default start from 0)
INT iStart (0), IEND (0);
//
Find words
//
In the case of the search words discussed above, we only need to modify this section, you can achieve it.
//
Treated with the format type word, in order to better versatility, it is best to find some words.
//
As a single function, or a class to process
For (int i = 0; i { // Find space split symbols IF (STRSOUCE [I] == ') { // Find a word IEND = I-1; // For only one character, there is no need to reverse it (i) IF (iStart { // Inversion word _Reversalchar (STRSOUCE, ISTART, IEND); } // Record the beginning of the next word iStart = i 1; } // Several common punctuation symbols Else IF (strsouce [i] == '!' || strsouce [i] == ',' || strsouce [i] == '.') { iStart = i 1; } } // Returns the string after reverse Return STRSOUCE; } Test the above method: Char ch [] = "I am a student !!"; COUT << "Source String:" << CH << Endl; Cout << "Reversal String:" << REVERCHAR (AA) << ENDL; Screen printing characters Source String: I am A student !! Reversal string: !! Student a am i Above code test environment: Windows2003 VC7.1, if used directly under TC, you need to assign the Int IStart (0) type variable, modify Int iStart = 0; type. There are many ways to solve this problem. For example, it can use a binary tree to store words, and then use different methods of follow-up to traverse, reverse the word order, usually use a two-way linked list to realize words reversal, there are many ways to solve this problem, these methods There is no good bad points itself, the key is that if this algorithm is suitable for our situation, then this algorithm is the best. For example, passing to us a paragraph of a two-way linked structure, let us reverse, then only we need to read it before. In fact, if this entitled changes to every word in a paragraph, there will be a lot more difficult. I don't know how to tell the new words