Flip word order

Source: Internet
Author: User
Enter an English sentence to flip the order of words in the sentence, but the order of Characters in the word remains unchanged. For example, the input character "student. A am I" is output as "I am a student.". This topic is very common. It is said that many companies have used it for interview questions multiple times. This is mainly because many of the relevant code on the internet is copied by HE Haitao's offoffoffer, and the c ++-style code is unacceptable, so it's easy to post my AC code ideas here, just in two steps:
  1. Flip all characters in a sentence, for example, flip "student. all characters in a am I get "I ma. tneduts ", which not only flipped the order of words in the sentence, but also flipped the character order in the word.
  2. Flip the character order in each word to get "I Am a students .".
The Code Implementation of string flip is very simple. I posted the C code I wrote:
void reverse_word(char *str, int begin, int end){char temp;while (begin < end) {temp = *(str + begin);*(str + begin) = *(str + end);*(str + end) = temp;begin ++;end --;}}
Example
Description: jobdu recently came to a new employee, Fish. Every morning, he always holds an English magazine and writes some sentences on the book. Cat, a colleague, is very interested in the content written by fish. One day he borrowed it from fish but did not understand it. For example, "student. A am I ". Later, I realized that this guy turned the order of sentence words. The correct sentence should be "I am a student .". Cat is not able to flip these words one by one. Can you help him? Input: one line for each test case, indicating an English sentence. We guarantee that the number of words in a sentence cannot exceed 600, and the length of each word cannot exceed 30. However, it should be noted that fish is an informal person. Sometimes there may be many spaces in the middle of two words. For convenience, you can think that the total number of characters in a line is no more than 50000, And the punctuation can be processed like a regular letter. Output: For each test case, output a single line of correct sentences after turning. Sample input: Student. A am II'm a freshman and I like jobdu! Sample output: I am a student. jobdu! Like I and freshman a I'm

AC code

#include <stdio.h>#include <string.h> void reverse_word(char *str, int begin, int end); int main(void){    int st, ed, len;    char str[50001];     while (gets(str)) {        len = strlen(str);        reverse_word(str, 0, len - 1);         for (st = ed = 0; ed < len;) {            while (*(str + ed) != ' ' && *(str + ed) != '\0') {                ed ++;            }            reverse_word(str, st, ed - 1);                         st = ed;             while (*(str + st) == ' ') {                st ++;            }            ed = st;        }         puts(str);    }     return 0;} void reverse_word(char *str, int begin, int end){    char temp;     while (begin < end) {        temp = *(str + begin);        *(str + begin) = *(str + end);        *(str + end) = temp;        begin ++;        end --;    }   }/**************************************************************    Problem: 1361    User: wangzhengyi    Language: C    Result: Accepted    Time:50 ms    Memory:912 kb****************************************************************/

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.