#include<stdio.h>
#include<string.h>
main()
{
char STR1[100];
int i;
clrscr();
printf("Please input a STRING.\n");
gets (STR1);
printf("\n%s is displayed in reverse form as:\n",STR1);
for(i=strlen(STR1)-1; i>=0 ; i--)
    {
    printf("%c",STR1[i]);
    }
getch();
}

Recommended Answers

All 3 Replies

Let's see...
No CODE tags, even thought there are at least 6 places they are explained, and 3 of them on the main page alone.
No explanation about why code was posted
I guess all there is to say is:
Congrats for writing some code! What's next?

commented: Persistance against a never ending wad of crap posts. +2
main()

If you do not specify a return type for main(), it does not mean you are exempt from returning a value. This feature is called implicit int. It means that if a data type is expected but not provided, int is assumed. So these two definitions of main() are exactly the same and the return statement is required for both:

main()
{
    return 0;
}
int main()
{
    return 0;
}

Relying on implicit int is not a good practice because the latest C standard removes the feature. Your code will not compile as C99, and that adds one more thing that needs to be changed to make it conform.

char STR1[100];

Names in all upper case are usually reserved for macros. It is up to you whether or not to use that convention, but people reading your code will be confused that STR1 is an array object and not a macro.

clrscr();

clrscr() is a non-standard function from conio.h, but you did not include conio.h. I would not recommend anything from conio.h, but like the naming conventions, it is up to you whether or not to take that advice. The portability police will not haul you away if you choose to use unportable stuff. :)

gets (STR1);

gets() is always unsafe and cannot be made safe through good coding practices. Never use it, because there is always a risk of array overflow. The safe alternative to gets() is fgets():

fgets(STR1, sizeof STR1, stdin);
for(i=strlen(STR1)-1; i>=0 ; i--)
{
    printf("%c",STR1[i]);
}

In C you should always make sure the output stream is flushed by either printing a line break or calling the fflush() function. It is possible that the output for your program will not show up before getch() starts waiting for input. It will look like the program is hanging. You can easily fix the problem and make the code work everywhere by printing a line break after the loop:

for(i=strlen(STR1)-1; i>=0 ; i--)
{
    printf("%c",STR1[i]);
}

putchar('\n');
getch();

getch() is in the same category as clrscr() except there is a portable function from stdio.h that can replace getch() called getchar(). The only difference is that getchar() will probably not return until you type [Enter].

mr. tommy gunn...!!! thank you very much... im just a newbie user here but still you help me understand those things and you really helped me a lot unlike other users... thank you very much...

commented: Other users are trying to help you with the rules of the forum. You're just luck Mr Gunn is a good guy. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.