Home » Reverse a String in C

Reverse a String in C

by Online Tutorials Library

Reverse a String in C

This topic will discuss several ways to reverse a string in the C programming language. Reversing a string is the technique that reverses or changes the order of a given string so that the last character of the string becomes the first character of the string and so on. Furthermore, we can also check the Palindrome of the given string by reversing the original string.

For example, we enter a string “APPLE“, and then use the reverse algorithm. The reverse algorithm returns the string “ELPPA” which is completely reverse of the original string.

Reverse a String in C

Different ways to find the reverse of a string in the C

Following are the various ways to find the reverse of a string in the C programming language:

  1. Reverse a string using the strrev() function
  2. Reverse a string without using the library function
  3. Reverse a string using the recursion function
  4. Reverse a string using for loop
  5. Reverse a string using while loop
  6. Reverse a string using pointers
  7. Reverse a string to check for Palindrome

Program 1: Print the reverse of a string using strrev() function

Let’s consider an example to print the reverse of a string using the strrev() function.

Program1.c

Output

Enter a string to be reversed: AMBULANCE   After the reverse of a string: ECNALUBMA  

Program 2: Print the reverse of a string without using the library function

Let’s consider an example to print the reverse of a string using user defined function.

Program2.c

Output

Enter the string: Welcome Friends     Before reversing the string: Welcome Friends   After reversing the string: sdneirF emocleW  

Program 3: Print the reverse of a string using the recursion function

Let’s consider an example to print the reverse of a string using the recursion function.

Recursion function: A recursion function is a function that continuously calls the same function without using a looping statement.

Program3.c

Output

Enter the string: LIFE INSURANCE     Before reversing the string: LIFE INSURANCE   After reversing the string: ECNARUSNI EFIL  

Program 4: Print the reverse of a string using for loop

Let’s consider an example to print the reverse of a string using for loop in C programming language.

Program4.c

Output

Display a reverse string in the C:   -----------------------   Enter a string to reverse order: APPLE   The reverse of the original string is: ELPPA  

Program 5: Print the reverse of a string using while loop

Let’s consider an example to print the reverse of a string using while loop in C programming language.

Program5.c

Output

Enter a string to be reversed: tutoraspire   The reversed of the string: TNIOPTAVAJ  

Program 6: Print the reverse of a string using pointers

Let’s consider an example to print the reverse of a string using pointers in the C programming language.

Program6.c

You may also like