Reversing a character string without using library function “strrev()”


This post is in response to my friend Anubhav Singh‘s query regarding how to reverse a character string without using the standard library function strrev(), and is a very common question asked in the technical round for TCS, Wipro, Infosys etc.

Hope this will help!!

/* Webdevil’s BLOG POST */
#include<stdio.h>
#include<string.h>
main()
{
char str[50], revstr[50];  //declare a character array to store the input string and reverse string
int i=0,j=0;
printf(“Enter the string to be reversed : “);
scanf(“%s”,str);
for(i=strlen(str)-1;i>=0;i–)
{
revstr[j]=str[i];
j++;
}
revstr[j]=”;
printf(“Input String : %s”,str);
printf(“\nOutput String : %s”,revstr);

}

The output will be like this ->

Queries are most welcome…!!