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 recursion.

Method 3: Reversing the string using standard library functions.

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 – 1
  • Repeat from i = 0 to length
  • rev[i] = str[temp]
  • temp – 1
  • Print the string

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

#include <stdio.h>
int main()
{
char str[1000], rev[1000];
int i, j, count = 0;
scanf("%s", str);
while (str[count] != '\0')
{
count++;
}
j = count - 1;
for (i = 0; i < count; i++)
{
rev[i] = str[j];
j--;
}
printf(rev);
}
#include <iostream>
using namespace std;
int main()
{
char str[1000], rev[1000];
int i, j, count = 0;
cin >> str;
while (str[count] != '\0')
{
count++;
}
j = count - 1;
for (i = 0; i < count; i++)
{
rev[i] = str[j];
j--;
}
cout << rev;
}
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();
for(int i = str.length() - 1; i >= 0; i--)
{
sb.append(str.charAt(i));
}
System.out.println(sb.toString());
}
}
str = input()
rev = ""
num1 = len(str);
j = num1 - 1
for i in range(0, num1):
rev = rev + str[j]
j = j - 1
print(rev)

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];
scanf("%s", a);
reverse(a, 0, strlen(a) - 1);
printf(a);
return 0;
}
#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];
cin >> a;
reverse(a, 0, strlen(a) - 1);
cout << a;
return 0;
}
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));
}
}

Program to reverse a string using 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