Reverse a string

Problem 65: Reverse
Take a word from user and print the word in reverse order. [If user gives  “BANGLADESH”, your program should print “HSEDALGNAB”]

code:

#include<stdio.h>
int main()
{
    char str[20];
    int i,j;
    printf("enter string here:");
    gets(str);
    for(i=0; str[i]!='\0'; i++);
    printf("reverse order:");
    for(j=i-1; j>=0; j--)
    {
        putchar(str[j]);
    }

    return 0;
}

of another option:

#include<stdio.h>
int main()
{
    char str[20];
    int i,j;
    printf("enter string here:");
    gets(str);
    for(i=0; str[i]!='\0'; i++);
    printf("reverse order:");
    for(j=i-1; j>=0; j--)
    {
        printf("%c",str[j]);
    }

    return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *