How to write a Program to Reverse the given string in C

Write a C program to reverse the string without using strrev() function.This program reverses a string entered by the user. For example if a user enters a string “reverse me” then on reversing the string will be “em esrever”. We show you three different methods to reverse string the first one uses strrev library function of string.h header file and in second we make our own function to reverse string using pointers, reverse string using recursion and Reverse words in string. If you are using first method then you must include string.h in your program.

 

Example:-Vivek(Input)- keviV(Output).

#include
#include
main()
{
char str[50],revstr[50];
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);
getch();
}

Leave a Reply