Program to print a string in reverse order.

Here, we have a basic program example to reverse a string using different languages. This program is created in c language, c++, Java, and Python.

Code to reverse string in C language

#include <stdio.h>
#include <string.h>
void main()
{
   char str[100], tmp;
   int l, lind, rind,i;

       printf("\n\nPrint a string in reverse order:\n ");
       printf("-------------------------------------\n");

   printf("Input a string to reverse : ");
   scanf("%s", str);
   l = strlen(str);

   lind = 0;
   rind = l-1;

for(i=lind;i<rind;i++)
       {
       tmp = str[i];
       str[i] = str[rind];
       str[rind] = tmp;
       rind--;
   }
   printf("Reversed string is: %s\n\n", str);
}

Code to reverse string in C++ language

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char str[100], tmp;
    int l, lind, rind,i;

       cout<<"\n\nPrint a string in reverse order:\n ";
       cout<<"-------------------------------------\n";

   cout<<"Input a string to reverse : ";
   cin>>str1;
   l = strlen(str);

   lind = 0;
   rind = l-1;

for(i=lind;i<rind;i++)
       {
       tmp = str[i];
       str[i] = str[rind];
       str[rind] = tmp;
       rind--;
   }
   cout<<"Reversed string is: \n"<<str;
}

Code to reverse string in Python language

print("\n\nPrint a string in reverse order:\n ");
print("-------------------------------------\n");
str=input("Input a string:  ")
rev=""
for i in str:
    rev=i+rev
print("The reversed string is: ", rev)

Code to reverse string in Java language

import java.util.Scanner;
public class reverse {
     public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Input a string: ");
        char[] letters = scanner.nextLine().toCharArray();

        System.out.print("Reverse string: ");
        for (int i = letters.length - 1; i >= 0; i--) {
            System.out.print(letters[i]);
        }
        System.out.print("\n");
    }
}