C Program to Reverse a String using Stack, Pointers, Recursion & Swap

Write a c program to reverse a string without using strrev function. Given an input string, we have to write a method to reverse a string. In c, you can simply reverse a string using an inbuilt strrev() function. But we don’t have to use this method.

Think how do you solve this problem. In this tutorial, we write a c code to reverse a string using stack, pointers, recursion, loop.

C Program to Reverse a String

C Program to Reverse a String

C Program to Reverse a String using Stack

In first example, we write c program to reverse a string using stack. Stack is an important data structure in computer science. If you are not familiar with stack data structure then you can check my previous post on  what is stack and how to implement stack using array.

Stack implementation using Linked List

Subscribe Our Tutorials

Get Latest Updates on Facebook

In this program, we first push all the characters of a string in a stack. Once all the characters are pushed then we pop the character and print it. This operation will print the string in reverse order .

Implement Stack using Array

Reverse String by Swapping their Position

In this approach we reverse a  string by swapping their position. For this let’s take two indexes (first and last) .

a) Initialize first index with 0 and last index with arraylength-1 .

b) In next step swap the position of character occur in first and last index. Repeat this process until first index is less than or equal to last index.

C Program to Reverse a String using Swap

We have seen a code example to reverse a string using stack. In this code example, we reverse a string using swap.

C Program to swap two numbers without using third variable

C program to swap two numbers using third variable

C Program to Reverse a String using Pointers

In this code example, we use pointers to reverse a string.

C Program to Reverse a String using Recursion

In this last example, we are going to write a c code to reverse a string using recursion.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.