Code: reverse the string.

Source: Internet
Author: User
Reverse the string 《 Microsoft interview questions -- reverse stringIt provides the implementation and analysis of a simple string inversion, probably because of the two characters of Microsoft, which has aroused a lot of interest and concern :), in fact, this is also the written examination or interview questions of many enterprises (or some variants based on this ). The question mentioned in the reply should be reversed by words in the string, for example, "I love you" to "you love I ", it should be noted that the original question is to simply reverse the content of the entire string as a whole, but I think it is also meaningful and useful to reverse the content according to the words in the string, so I wrote an implementation :).

The idea of implementation is very simple. First, reverse the entire string and then find the word inversion.

The following code uses this method:
 
/*************************************** *********************************/
// 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 determining the character '/0'.
/*************************************** *********************************/
Int ustrlen (const char * strsource)
{
// Declare Variables
Int ilength (0 );
// Traverse the string and search for 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

 

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.