Reverse string by word

Source: Internet
Author: User
Http://topic.csdn.net/t/20060621/16/4834987.html
Http://www.zhuxinquan.com/cn/2006/03/post_7.html
Http://blog.csdn.net/jiyucn/archive/2006/06/16/803059.aspx
Http://www.baihowff.com/post/145.html
There are many ways to reverse word order in a string. we can define a stack structure, based on the characteristics of the stack, first come out later. We look for spaces in sequence (this is only the simplest condition in the actual word analysis application. words may be distinguished directly by punctuation marks, but the use of punctuation marks does not mean that they are two words, in the Western world, counting methods like to use three digits plus a comma, for example, "3,483,123". Although we can find "," We cannot distinguish "3,483,123" as three words, it should be considered as a word. If we think about Chinese and English characters, it will be more complicated to distinguish words. Fortunately, we don't need to consider such a complicated situation here) to distinguish words, then, each word is pushed to the stack in sequence, and then each word is read from the stack in sequence to reverse the word order of the entire string. However, we have to consider three issues in the specific implementation process:
1: we need to define a stack structure to save every word, and maintain, manage, and fill in the stack structure during string inversion, we also need to determine the number of characters in each request to fill the word so as to dynamically apply for space to save the word. After we use it, we need to delete the structure to prevent memory leakage. In this way, our main focus will be on maintaining this stack, rather than reversing the word order.
2: During word analysis, if we press the directly analyzed words on the stack and then output them, we simply add spaces between the two words, the reverse string may not match the original string. For example, "I am a student" A and student are two spaces, the string after the inverted word is output in the above way is "student a am I" and there is only one space between student and a, which may not be obvious here, if the original string uses punctuation to distinguish two words, And we fill in spaces, the resulting string may be significantly different from the true inversion of the original string. For example, "Student A, am I" is reversed to "I am a student" instead of "I am, a student ". In the face of such problems, you have to define a stack structure to store the relationship between two words, or save spaces or punctuation marks together if the words that were originally written into the stack are not really meaningful words.
3: Because we use the stack to store reverse word data, the stack is cleared once every time the reverse data is used. Before the next use, we must analyze the word and re-import it to the stack. To avoid this problem, you can use an array stack, that is, add an index operation to the stack to index the data in the reverse order of the inbound stack, but unfortunately this step is done, you don't have to re-Modify your stack structure.
Considering the stack structure, we have to spend too much effort to consider the stack structure and maintain the stack during use. after use, we delete the stack structure to prevent resource leakage. After the stack structure is used, Algorithm It does not belong to the "in-situ" algorithm. At least one time of the string space is used. This method is not applicable in memory-intensive systems (such as embedded systems. However, this method does not have no advantages. The advantage is that you only need to traverse the string (a paragraph) to obtain the reversed string.
Here we will explain another method: the reverse string method is used to reverse word order. Basic principle: first, we will reverse the entire string to be reversed (a paragraph). For example, "I am a student" is reversed to "tneduts a ma I", and then word-by-word inversion, finally, we get "student a am I ". The advantage of using this method is that we use an inversion algorithm instead of the stack structure. This algorithm is a "in-situ" algorithm and does not need to apply for space, as long as the memory can hold character strings, word inversion can be achieved. Disadvantage: You have to write another function to get the string length and traverse the string almost twice.



The following describes how to use this method. Code :

/*************************************** *********************************/
// Function name: ustrlen
// Input parameter: strsource, string to be evaluated;
// Output parameter: int, the length of the string.
// Description: the string length is obtained by judging the character '\ 0'.
/*************************************** *********************************/
Int ustrlen (const char * strsource)
{
// Declare Variables
Int ilength (0 );
// Traverse the string and find the character '\ 0'
While (* strsource ++! = '\ 0 ')
{
++ Ilength;
}
// Returns the length of the string.
Return ilength;
}
/*************************************** *********************************/
// Function name: _ reversalchar
// Input parameter: strsouce, string to be reversed; istart, starting position of the string to be rotated; iend, ending position of the string to be rotated
// Output parameter: char *. returns the pointer to the string after the inversion;
// Description: reverse the string between istart and string iend.
/*************************************** *********************************/
Char * _ reversalchar (char * strsouce, int istart, int iend)
{
// Reverse the 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
// Input parameter: strsource, string to be reversed
// Output parameter: char *, returns the pointer after the string is reversed.
// Description: reverse the string by word
/*************************************** *********************************/
Char * reversalchar (char * strsouce)
{
// Obtain the length of the string
Int ilength = ustrlen (strsouce );

// Reverse the entire string
_ Reversalchar (strsouce, 0, iLength-1 );

// Declare the variable (the start and end of a word start from 0 by default)
Int istart (0), iend (0 );

// Search for words
// As mentioned in the search for words above, you only need to modify this part
// Process words in the same format. For better versatility, it is best to search for words.
// Process as a single function or a class
For (INT I = 0; I <ilength; ++ I)
{
// Search for space-separated symbols
If (strsouce [I] = '')
{
// Find a word
Iend = I-1;
// For words with only one character, such as (I), there is no need to reverse.
If (istart <iend)
{
// Reverse the word
_ Reversalchar (strsouce, istart, iend );
}
// Record the start position of the next word
Istart = I + 1;
}
// Special handling of several common punctuation marks
Else if (strsouce [I] = '! '| Strsouce [I] =', '| strsouce [I] = '.')
{
Istart = I + 1;
}
}
// Returns the reversed string.
Return strsouce;
}

Test the above method:
Char ch [] = "I am a student !! ";
Cout <"Source string:" <ch <Endl;
Cout <"reversal string:" <reversalchar (AA) <Endl;
Screen Print characters
Source string: I am a student !!
Reversal string :!! Student A am I

Test environment of the above Code: Windows2003 + vc7.1. If you use it directly under TC, You need to assign a value to the int istart (0) type variable, and change it to int istart = 0; type.

There are many ways to solve this problem. For example, you can use a binary tree to store words, and then use different methods for subsequent traversal to reverse word order. Generally, you can use a two-way linked list to reverse word order, there are many ways to solve this problem. These methods are not good or bad. The key is that if this algorithm is suitable for us, this algorithm is the best. For example, if we pass a line of a two-way linked list structure to reverse order, we only need to read from the back and forward.

In fact, it would be much more difficult to analyze every word in a passage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.