944,101 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 678
  • C RSS
Oct 15th, 2009
0

typed letters or numbers are reversed-"string"

Expand Post »
#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();
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chescarleta18 is offline Offline
9 posts
since Oct 2009
Oct 15th, 2009
0
Re: typed letters or numbers are reversed-"string"
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?
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 16th, 2009
1
Re: typed letters or numbers are reversed-"string"
Quote ...
  1. 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:
  1. main()
  2. {
  3. return 0;
  4. }
  1. int main()
  2. {
  3. return 0;
  4. }
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.

Quote ...
  1. 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.

Quote ...
  1. 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.

Quote ...
  1. 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():
  1. fgets(STR1, sizeof STR1, stdin);
Quote ...
  1. for(i=strlen(STR1)-1; i>=0 ; i--)
  2. {
  3. printf("%c",STR1[i]);
  4. }
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:
  1. for(i=strlen(STR1)-1; i>=0 ; i--)
  2. {
  3. printf("%c",STR1[i]);
  4. }
  5.  
  6. putchar('\n');
Quote ...
  1. 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].
Last edited by Tom Gunn; Oct 16th, 2009 at 10:54 am.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 19th, 2009
-1
Re: typed letters or numbers are reversed-"string"
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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chescarleta18 is offline Offline
9 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Problem with my Code
Next Thread in C Forum Timeline: C problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC