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

In this article, we will discuss different methods to reverse a string in C/C++/Java/Python. A string can be reversed using the following approaches.

  • Method 1: By swapping the characters of the string
  • Method 2: By using recursion
  • Method 3: By using standard library functions

Consider the below I/O samples before you program to reverse a string.

SAMPLE INPUT 1: faceprep
SAMPLE OUTPUT 1: perpecaf

SAMPLE INPUT 2: welcome
SAMPLE OUTPUT 2: emoclew

Method 1: Program to reverse a string by swapping the characters

The algorithm used in this method to reverse a given string is:

  • Input the string from the user
  • Find the length of the string. The actual length of the string is one less than the number of characters in the string. Let actual length be j.
  • Repeat the below steps from i = 0 to the entire length of the string.
  • rev[i] = str[j]
  • Print the reversed string.

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

//program to reverse a string in c

#include <stdio.h>
int main()
{
char str[1000], rev[1000];
int i, j, count = 0;
scanf("%s", str);

#finding the length of the string
while (str[count] != '\0')
{
count++;
}
j = count - 1;

#reversing the string by swapping
for (i = 0; i < count; i++)
{
rev[i] = str[j];
j--;
}

printf(rev);
}
//program to reverse a string in C++

#include <iostream>
using namespace std;
int main()
{
char str[1000], rev[1000];
int i, j, count = 0;
cin >> str;

//finding the length of the string by counting
while (str[count] != '\0')
{
count++;
}
j = count - 1;

//reversing the string by swapping
for (i = 0; i < count; i++)
{
rev[i] = str[j];
j--;
}
cout << rev;
}
//program to reverse a string in Java

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuilder sb = new StringBuilder();

#Appending the characters in the reverse order
for(int i = str.length() - 1; i >= 0; i--)
{
sb.append(str.charAt(i));
}
System.out.println(sb.toString());
}
}
#program to reverse a string in Python

str = input()
rev = ""

#finding the string length
num1 = len(str);
j = num1 - 1

#
for i in range(0, num1):
rev = rev + str[j]
j = j - 1
print(rev)

Method 2: Program to reverse a string using recursion

//program to reverse a string using recursion in C

#include <stdio.h>
#include <string.h>

#function to reverse the string
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];
scanf("%s", a);
reverse(a, 0, strlen(a) - 1);
printf(a);
return 0;
}
//program to reverse a string using recursion in C++

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

#function to reverse a string
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];
cin >> a;
reverse(a, 0, strlen(a) - 1);
cout << a;
return 0;
}
//program to reverse a string using recursion in Java

import java.util.Scanner;
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)
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(reverse_string(str));
}
}

Method 3: Program to reverse a string using the standard library function

#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin >> str;
reverse(str.begin(), str.end());
cout << str << endl;
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuilder input = new StringBuilder();
input.append(str);
input = input.reverse();
System.out.println(input);
}
}
str = input()
s1 = "".join(reversed(str))
print(s1)

INPUT:
programs

OUTPUT:
smargorp