C Program - Reverse a given String
In C, the reverse of a given string can be found out by using below mentioned methods.
Method 1: Using iteration
In the example below, the string called MyString is reversed using ReverseString() function. Two variables l and r are created which initially points to first and last character of the string. If l is less than r, then the characters of MyString at indices l and r swapped. After swapping, l is increased by one and r is decreased by one until they cross each other. Finally, MyString will contain the reversed value of the initial string.
#include <stdio.h> #include <string.h> static void ReverseString(char MyString[]) { int l = 0; int r = strlen(MyString) - 1; while(r > l){ //swapping characters at indices l and r char c = MyString[l]; MyString[l] = MyString[r]; MyString[r] = c; l++; r--; } printf("%s \n", MyString); } int main() { char a[] = "Hello World"; char b[] = "Programming is fun"; char c[] = "Reverse this string"; ReverseString(a); ReverseString(b); ReverseString(c); return 0; }
The above code will give the following output:
dlroW olleH nuf si gnimmargorP gnirts siht esreveR
Method 2: Using Recursion
The above result can also be achieved using recursive function. Consider the example below:
#include <stdio.h> void ReverseString(char *MyString) { if(*MyString) { ReverseString(MyString + 1); printf("%c", *MyString); } } int main() { char a[] = "Hello World"; char b[] = "Programming is fun"; char c[] = "Reverse this string"; ReverseString(a); printf("\n"); ReverseString(b); printf("\n"); ReverseString(c); printf("\n"); return 0; }
The above code will give the following output:
dlroW olleH nuf si gnimmargorP gnirts siht esreveR
Method 3: Printing the string in reverse order
The same can be achieved by printing the string in reverse order. Consider the example below:
#include <stdio.h> #include <string.h> static void ReverseString(char MyString[]) { int last = strlen(MyString) - 1; for(int i = last; i >= 0; i--) { printf("%c", MyString[i]); } printf("\n"); } int main() { char a[] = "Hello World"; char b[] = "Programming is fun"; char c[] = "Reverse this string"; ReverseString(a); ReverseString(b); ReverseString(c); return 0; }
The above code will give the following output:
dlroW olleH nuf si gnimmargorP gnirts siht esreveR
Recommended Pages
- C - Swap two numbers
- C Program - Fibonacci Sequence
- C Program - Insertion Sort
- C Program - Find Factorial of a Number
- C Program - Find HCF of Two Numbers
- C Program - Merge Sort
- C Program - Shell Sort
- Stack in C
- Queue in C
- C Program - Find LCM of Two Numbers
- C Program - To Check Whether a Number is Palindrome or Not
- C Program - To Check Whether a String is Palindrome or Not
- C Program - Heap Sort
- C Program - Quick Sort
- C - Swap Two Numbers without using Temporary Variable
- C Program - To Check Armstrong Number
- C Program - Counting Sort
- C Program - Radix Sort
- C Program - Find Largest Number among Three Numbers
- C Program - Print Floyd's Triangle