C program to print given string in reverse order without using strrev()

Ex: Write a C program to print given string in reverse order without using strrev(). How to write a C program to print given string in reverse order without using strrev(). C program to print given string in reverse order without using strrev(). 

Input from user:

Enter the string:
codeforhunger

Expected output:

Given string in reverse order:
regnuhrofedoc




  Step by step logic of the given program:


1. Accept string from user declare varible say str.

2. After that calculate length using strlen() function and store it in variable len.

3. Run for loop from length of string to 0 which is looks like:
for(i=len;i>=0;i--)

4. Inside the for loop print string characters one by one.





  Program to print given string in reverse order without using strrev():

#include<stdio.h>
#include<string.h>
#define max 100

void main()
{
char str[max];
int i,len=0;

    /*Accept string*/
printf("Enter the string:\n");
gets(str);

len=strlen(str);

printf("Given string in reverse order: \n");
/*print given string in reverse order*/
for(i=len;i>=0;i--)
{
printf("%c",str[i]);
}
}



Above program shows the following output:


C program to print given string in reverse order without using strrev(), C Program to reverse a String




No comments:

Post a Comment

If you have any doubts, please discuss here...👇