3 ways to reverse string in C + +

Source: Internet
Author: User

First: use the reverse function in algorithm

#include <iostream> #include <string> #include <algorithm>using namespace Std;int main () {    string s = "hello";    Reverse (s.begin (), s.end ());    cout<<s<<endl;    Return 0;}

The second type: write it Yourself

#include <iostream>using namespace std;void Reverse (char *s,int n) {for    (int i=0,j=n-1;i<j;i++,j--) {        Char c=s[i];        s[i]=s[j];        s[j]=c;}    } int main () {    char s[]= "hello";    Reverse (s,5);    cout<<s<<endl;    Return 0;}

The third type: using the Strrev function in String.h

#include <iostream> #include <cstring>using namespace Std;int main () {    Char s[]= "hello";    Strrev (s);    cout<<s<<endl;    Return 0;}

  

3 ways to reverse string in C + +

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.