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

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. The process of reversing a string involves changing the order of the characters in the string so that the last character becomes the first character, and so on a string can be reversed using the following approaches.

Methods to reverse a string in C, C++, Java and Python


  • 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: 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 the 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.


face prep pro ad bannerClick here to learn more about FACE Prep PRO


Program to reverse a string by swapping the characters using the iterative approach

@@coding::1@@


Method 2: Program to reverse a string using recursion

@@coding::2@@


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

@@coding::3@@


Recommended Programs


Methods to reverse a string in C, C++, Java and Python

c