#include <stdio.h>
#include <stdlib.h>

void main()
{
    char a[50];
    int n;
    printf("enter the string  : ");
    gets(a);
    n=strlen(a);
    printf("%d",n);
for(i=n;i>0;i--)
    {
        printf("%c",a[i]);
    }



}

the error is at i=n why cant we use i=n;

And what part of "*don't use gets() *" is so difficult to understand?

If you want further help, start using the help you've been given. Otherwise, why should we bother?

Hi Rithsh,

the error is at i=n why cant we use i=n;

n is string length value say (string is a[] = Test, lenght n=4). Characters will start at index 0 so string starts like a[0]='T', a[1]='e', a[2]='s', a[3]='t', a[4]='\0'. When you try to print a[4] character then '\0' will print and you need to go till a[0]
I have modified your code with bit changes as below.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char a[50];
    int n, i = 0;
    printf("enter the string  : ");
    gets(a);//Read string
    n=strlen(a);//Find length of string
    printf("The lenght of string is : %d \n",n);
    printf("The reversed string is : ");
    for ( i = (n-1); i>=0; i-- )
    {
        printf("%c",a[i]);//Printing character from last 
    }

}

Edited 4 Years Ago by Perry31

ok waltp as u say i wont use gets() but using gets() its easy to read a string right so what else can i use scanf????

and perry 31 i tried ur code its producing wrong output but does not show any error

Rithish,
Brief me the wrong output with some example. i dont find any wrong output with this.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char a[50];
    int n, i = 0;
    printf("enter the string  : ");
    gets(a);
    n=strlen(a);
    printf("The lenght of string is : %d \n",n);
    printf("The reversed string is : ");
    for ( i = (n-1); i>=0; i-- )
    {
        printf("%c",a[i]);
    }

}

my Input is : abc def ghi
output is : ihg fed cba

Edited 4 Years Ago by Perry31

ok waltp as u say i wont use gets() but using gets() its easy to read a string right...

Using a bulldozer to open your front door is easy, too. It that the safest tool?

... so what else can i use scanf????

here -- here -- here
Actually read the help that you have been given.

and perry 31 i tried ur code its producing wrong output but does not show any error

And this is one reason why we don't give away free code, as well as the reason it's called cheating... YOU don't get the grade, and THEY don't deserve the grade on your work.

Comments
damn funny comment!
This question has already been answered. Start a new discussion instead.