Word Flip (I am a student)--->> (Student a Am I)

Source: Internet
Author: User

Problem Description:

Write a function to convert I am a student to student a am I

Problem Solving Ideas:


I am a student--->--> student a am i#include<iostream>using namespace std;/* Flip Function---> Take advantage of Exchange */void Words_re Verse (char str[],int left,int right) {when (left < right) {Str[left] + = str[right];str[right] = str[left]-str[right];s Tr[left]-= str[right];left++;right--;}} int main () {char str[] = "Student a Am I";/* finds the array (leftmost) and (rightmost) after the overall rollover */int left = 0;int right = 0;while (Str[right]! = ' + ') {RI ght++;} Words_reverse (str,left,right-1);/* Find each word (leftmost) and (rightmost), flip each word */int lowflag = 0; Status flag bit: The left side of the string to invert is found int highflag = 0;//status flag bit: the right side of the string to invert is found int i = 0;while (str[i]! = ')} {/* Search for the leftmost, low */if of the word (Str[i]! = "&& Lowflag = = 0)//If no word is found on the leftmost premise, if a non-empty characters is found to indicate the leftmost side found {Lowflag = 1;left = i;} /*if (str[i] = = "&& Highflag = = 0)//char str[] =" I am a student "situation is not appropriate (the last side has a space) {Highflag = 1;right = i-1;} *//* search for the right and low */if of the word (Lowflag = = 1 && str[i] = = ' && highflag = 0)//char str[] = "Student a Am I"; A word does not flip) {Highflag = 1;//without finding the right side of the word, if a space character is found, the first word is the mostRight-hand character = i-1;} /* Leftmost (low) and rightmost (high) are found after completing the flip */if (Lowflag = = 1 && highflag = = 1) {words_reverse (str,left,right); lowflag = 0; Highflag = 0;} i++;} Cout<<str<<endl;return 0;}


Word Flip (I am a student)--->> (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.