C ++ inputs a string and parses the characters in the string in two reverse order.

Source: Internet
Author: User

The following describes how to input a string in C ++ and output the characters in descending order. For more information, see

Use the character array method:
The basic idea is to first judge the character ending sign ''and then output it from this position.
Implementation Code:

Copy codeThe Code is as follows:
# Include <iostream>
Using namespace std;
Int main (){
Char a [50];
Cout <"please input a string :";
Cin>;
Int I = 0, k = 0;
While (I <50 ){
If (a [I] = ''){
K = I;
Break;
}
I ++;
}
Cout <"reverse order :";
For (; k> = 0; k --){
Cout <a [k];
}
Cout <endl;
Return 0;

}


Use the string method:
The basic idea is to use the strlen () function to determine the length of a character and then output it from the position of the length of the array.
Implementation Code:

Copy codeThe Code is as follows:


# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Char a [50];
Cout <"please input a string :";
Cin>;
Int k = 0;
K = strlen ();
Cout <"Reverse order :";
For (; k> = 0; k --){
Cout <a [k];
}
Cout <endl;
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.