Review data structures and algorithms 02-reverse the order of words in a sentence (the Character Sequence in words remains unchanged)

Source: Internet
Author: User

The order of words in an English sentence is reversed, and the order of Characters in the word remains unchanged.

For example, set "Live and learn! "Reversed "! Learn and live"

Consider:
1. Reverse all characters in the entire sentence :"! Nrael DNA edevil"
2. Invert words one by one in a sentence :"! Learn and live "(separated by non-characters and numbers)

# Include "stdafx. H"
# Include <stdlib. h>
# Include <string. h>
# Include <ctype. h>

// Reverse the string start ~ Character Sequence of end
Char * strrev (char * Start, char * end)
{
If (Start> = end)
Return NULL;
Char * ret = start;
Char ch;
While (start <End)
{
Ch = * start;
* Start ++ = * end;
* End -- = CH;
}

Return ret;
}

// Reverse the word order in a sentence
Char * strrevword (char * Str)
{
If (STR = NULL)
Return NULL;
Char * ret = STR;
 
Int nlen = strlen (STR );
// Reverse the entire string
Strrev (STR, STR + nlen-1 );
Char * Start = STR;
While (* Str)
{
// Separate search characters
While (* STR & (isalpha (* Str) | isdigit (* Str )))
STR ++;
// Reverse the word
If (start <Str)
Strrev (START, str-1 );
// Skip non-characters and numbers
While (* STR &&! Isalpha (* Str )&&! Isdigit (* Str ))
STR ++;
Start = STR;
}
Return ret;
}

Int main (INT argc, _ tchar * argv [])
{
Char STR [] = "Live and learn !!! ";
Strrevword (STR );
Printf ("% s/n", STR );
System ("pause ");
Return 0;
}

 

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.