[Note] Backward output of words in a sentence

Source: Internet
Author: User
1 # Include <iostream>
2 # Include < String >
3 # Include <stack>
4
5 Using Namespace STD;
6 Int Main ()
7 {
8 String STR = " I live in cnblogs.com " ;
9 Stack < Char > Cstack;
10 For ( Int I = 0 , W = 0 ; I <Str. Length (); I ++)
11 {
12 If (STR [I] = ' ' | I = Str. Length ()- 1 )
13 {
14 If (I = Str. Length ()- 1 & STR [I]! = ' ' ) // Handle the case where the last digit is not a space.
15 Cstack. Push (STR [I]);
16 While (Cstack. Size ()> 0 ) // Reverse Order of each word
17 {
18 STR [w] = cstack. Top ();
19 Cstack. Pop ();
20 W ++;
21 }
22 W = I + 1 ; // Next bit processing
23 }
24 Else
25 {
26 Cstack. Push (STR [I]);
27 }
28 }
29 Cout <STR;
30 Return 0 ;
31 }

 

Requirement: backward output of words in a sentence

Input: I live in coblogs.com

Output: I edevil Ni MOC. sgolbnc

ThisProgramNo new memory space will be applied.

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.