Reverse the order of words in a sentence C language

Source: Internet
Author: User

Enter an English sentence, flipping the order of the words in the sentence, but the order of the characters in the word does not change. Words are separated by spaces in sentences.
for simplicity, punctuation is treated like ordinary letters.

For example, convert "I am a student" to "Student a AM I".

#include <stdio.h> #include <string.h>//reversal string char* Reverse (char *str, int len) {if (str = = NULL | | Len &L    t;= 0) return str;    Char *plast = str + len-1;    char *pbegin = str;        while (Pbegin < pLast) {char temp = *pbegin;        *pbegin = *plast;        *plast = temp;        + + Pbegin;    --PLast; } return str;}    Reversal sentence char * reversesentence (char *str) {if (str = = NULL) {return str;    }//Reverses the entire sentence Reverse (str, strlen (str));    Reverses each word char *pbegin = str;    char *pend = str;        while (*pend! = ')} {while (*pend! = ' && *pend! = ') ++pend;        Reversal Word Reverse (pbegin, Pend-pbegin);        if (*pend = = ' + ') {break;        } ++pend;    Pbegin = pEnd; } return str;}    int main () {char str1[] = "";    Char str2[] = "a";    Char str3[] = "ABC";    Char str4[] = "I am a student";   if (strcmp (Reversesentence (STR1), "") = = 0         || strcmp (Reversesentence (STR2), "a") = = 0 | | strcmp (Reversesentence (STR3), "abc") = = 0 | |    strcmp (Reversesentence (STR4), "Student A AM I") = = 0) {printf ("Reverse sentence ok\n"); } 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.