Write a Program in C++ & Python to Reverse a String

Posted in

Write a Program in C++ & Python to Reverse a String

Vinay Khatri
Last updated on June 10, 2022

    In this program, we will ask the user to enter a string and print out that string in reverse order.

    C++ program to reverse a string:

    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    void main() {
         clrscr();
         char ch[50];
         cout<<"Enter a String: ";
         cin>>ch;
         cout<<"The reverse string is: ";
         for(int i=strlen(ch); i >= 0;i--)
             cout<<ch[i];
         getch();
    }

    Output:

    Enter a String: tech
    The reverse string is: hcet

    Python program to reverse a string:

    ch= input("Enter a String: ")
    print("The reverse string is:",ch[::-1])

    Output:

    Enter a String: techgeekbuzz
    The reverse string is: zzubkeeghcet

    People are also reading:

    Leave a Comment on this Post

    0 Comments