Program to reverse a string in C, C++, Java and Python | faceprep

Program to reverse a string is discussed here. A string can be reversed using the following approaches.

Method 1: Reversing the string by swapping the characters of the string.

Method 2: Reversing the string using standard library functions.

Method 3: Reversing the string using recursion

For example, consider the following examples.

Input 1: faceprep
Output 1: perpecaf

Input 2: welcome
Output 2: emoclew

Algorithm to reverse a string

  • Input the string from the user.
  • Find the length of the string.
  • Assign temp = length.
  • Repeat from i = 0 to length
  • rev[i] = str[temp]
  • temp–
  • rev[i] = ‘\0’
  • Print the string.

Program to reverse a string by swapping the characters using the iterative approach is given below.

/* C program to reverse a string */
#include <stdio.h>

int main()
{
char str[1000], rev[1000];
int i, j, count = 0;

printf(“\nEnter the string : “);
gets(str);

while (str[count] != ‘\0’)
count++;

j = count – 1;

for (i = 0; i < count; i++) {
rev[i] = str[j];
j–;
}

rev[i] = ‘\0’;

printf(“Reverse of the string : %s\n”, rev);

return 0;
}

// C++ program to reverse a string
#include <bits/stdc++.h>
using namespace std;

void reverse_string(string& str)
{
int n = str.length();


for (int i = 0; i < n / 2; i++)
swap(str[i], str[n – i – 1]);
}

int main()
{
string str;
cout << “\nEnter the string : “;
cin >> str;
reverse_string(str);
cout << “\nReverse of the string : ” << str << endl;
return 0;
}

/* Java program to reverse a string */

import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.println(“Enter the string : “);

Scanner sc = new Scanner(System.in);
String str = sc.nextLine();

StringBuilder sb = new StringBuilder();

for(int i = str.length() – 1; i >= 0; i–)
{
sb.append(str.charAt(i));
}

System.out.println(“Reversed string is:”);
System.out.println(sb.toString());
}
}

# Python program to reverse a string

str = input(“Enter the string : “)
rev = “”
for i in range(0, len(str)):
rev = rev + str[j]
j = j – 1
print(“Reversed String : “,rev)

Program to reverse a string using recursion

/* C program to reverse a string using recursion */
#include <stdio.h>
#include <string.h>

void reverse(char *x, int begin, int end)
{
char c;

if (begin >= end)
return;

c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;

reverse(x, ++begin, –end);
}

int main()
{
char a[100];
printf(“\nEnter the string : “);
gets(a);
reverse(a, 0, strlen(a)-1);
printf(“Reversed string is %s\n”, a);
return 0;
}

/* C++ program to reverse a string using recursion */
#include <iostream>
#include <string.h>
using namespace std;

void reverse(char *x, int begin, int end)
{
char c;

if (begin >= end)
return;

c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;

reverse(x, ++begin, –end);
}

int main()
{
char a[100];
cout << “\nEnter the string : “;
cin >> a;
reverse(a, 0, strlen(a)-1);
cout << “Reversed string is ” << a << endl;
return 0;
}

/* Java program to reverse a string using recursion */

import java.util.*;
public class Main
{
public static String reverse_string(String rev)
{
if (rev.isEmpty()){
return rev;
}

rev.substring(1);
return reverse_string(rev.substring(1)) + rev.charAt(0);
}
public static void main(String[] args)
{
System.out.println(“Enter the string : “);

Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(“Reversed String : ” + reverse_string(str));

}
}

Program to reverse a string using standard library function

/* C++ program to reverse a string  */
#include <bits/stdc++.h>
using namespace std;

int main()
{
string str;
cout << “\nEnter the string : “;
cin >> str;
reverse(str.begin(), str.end());
cout << “\nReversed string is ” << str << endl;
}

/* Java program to reverse a string */

import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.println(“Enter the string : “);
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuilder input = new StringBuilder();
input.append(str);
input = input.reverse();
System.out.println(“Reversed String : ” + input);
}
}

# Python program to reverse a string

str = input(“Enter the string : “)
s1 = ”.join(reversed(str))
print(“Reversed String : “, s1)

Output: 

reverse a string