Follow the word to flip the string without changing the order of letters in the word

Source: Internet
Author: User

For this question, you must first flip the entire string by letter, and then flip the string within the word, focusing on splitting the word, set the first and last signs to indicate the start and end positions of a word. The first or single space of a consecutive space is the end of the previous word, the next position of the last consecutive space is the start of the next word.
# Include <iostream>
# Define for if (1)
Using namespace std;
 
// Inverse Function of words, transmitted using char array
Void WordDesc (char str []);
 
Void main (){
Char str [] = "I am a student ";
Cout <"before processing:" <str <endl;
WordDesc (str );
Cout <"after processing:" <str <endl;
}
 
Void WordDesc (char str []) {
Int len = strlen (str); // String Length
Char temp; // temporary character for exchange
 
// Replace all characters first
For (int I = 0; I! = Len/2; I ++)
{
Temp = str [I];
Str [I] = str [len-i-1];
Str [len-i-1] = temp;
}
 
// Replace the value with the characters in each word.
Int first (0); // mark the start subscript of a word
Int last (0); // mark the end subscript of a word
 
For (int I = 0; I! = Len; I ++)
{
// Encounter a space
If (''= str [I])
{
// Multiple consecutive Spaces
If (''= str [I-1])
{
// Move the header pointer and perform the next loop
First ++;
Continue;
}
 
// A word before a space
 
// Mark the end as the previous one
Last = I-1;
 
// Process words
For (int j = first; j! = Last-(last-first)/2; j ++)
{
Temp = str [j];
Str [j] = str [last-j + first];
Str [last-j + first] = temp;
}
 
// Move the flag header to the next one
First = I + 1;
}
 
// Not a space, but to the end
Else if (I = len-1)
{
// Mark the end as current
Last = I;
 
// Process words
For (int j = first; j! = Last-(last-first)/2; j ++)
{
Temp = str [j];
Str [j] = str [last-j + first];
Str [last-j + first] = temp;
}
}
 
// Neither space nor tail
Else
{
NULL;
}
}
}
Running result:

Author: "cainiao changes"

Related Article

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.