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 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
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 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include<stdio.h> #include<string.h> #define max 100 int top; char stack[max]; void push(char x){ //condition to check stack overflow if(top == max-1){ printf("stack overflow"); } else { /* push character in a string*/ stack[++top]=x; } } void pop(){ /* Removing an element from the stack. */ printf("%c",stack[top--]); } main() { /* Define string value. */ char str[]= "Programming"; int len = strlen(str),i; /* Push characters of a string in a stack */ for(i = 0; i < len; i++) { push(str[i]); } /* Pop characters from stack */ for(i=0; i<len; i++) { pop(); } } |
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 .
1 2 |
first = 0; last = strlen(str)-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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> #include <string.h> int main(void) { char str[] = "webrewrite"; int first = 0; int last = strlen(str)-1; char temp; /* Swap the position of an element. */ while (first <= last) { temp = str[first]; str[first] = str[last]; str[last] = temp; /* Increment first index and decrement last index. */ first++; last--; } printf("Reverse of a string is %s",str); return 0; } |
C Program to Reverse a String using Pointers
In this code example, we use pointers to reverse a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <stdio.h. int main(void) { char str[100] = "webrewrite"; char rev[100]; //Pointer of character type char *strptr = str; char *revptr = rev; int i = -1; /* Increment strptr pointer, until it reaches the terminating character of a string */ while(*strptr) { strptr++; i++; } //value of i is greater than zero while(i >= 0) { strptr--; //copy the value of strptr to revptr *revptr = *strptr; revptr++; --i; } *revptr='\0'; printf("Reverse of string is : %s",rev); return 0; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> void reverseString(char str[], int start, int end) { char temp = str[start]; str[start] = str[end - start]; str[end-start] = temp; if (start == end/2) { return; } //Recursive call reverseString(str, start+1, end); } int main(void) { char str[100] = "webrewrite"; int start = 0; int end = strlen(str) - 1; reverseString(str, start, end); printf("Reverse of string is : %s",str); return 0; } |