typed letters or numbers are reversed-"string"

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 9
Reputation: chescarleta18 is an unknown quantity at this point 
Solved Threads: 0
chescarleta18 chescarleta18 is offline Offline
Newbie Poster

typed letters or numbers are reversed-"string"

 
0
  #1
Oct 15th, 2009
#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();
}
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei
 
0
  #2
Oct 15th, 2009
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?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
1
  #3
Oct 16th, 2009
  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.

  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.

  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.

  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);
  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');
  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.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: chescarleta18 is an unknown quantity at this point 
Solved Threads: 0
chescarleta18 chescarleta18 is offline Offline
Newbie Poster
 
-1
  #4
Oct 19th, 2009
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...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 414 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC