471,873 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 software developers and data experts.

gets(), reverse(), and strlen() help

Hi, thanks in advance for the assistance.

I need to make a program that will take a string using gets() (I know gets() is bad, but it's what I'm supposed to use), and then print the message in reverse. One problem I had was that when I used gets(message);, the program just continued without letting me enter anything. I'm using while(strlen(gets(message)) == 0); right now and it seems to work, but I'd prefer to use gets(message); if I can.

In any case, here's my code so far:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. main(void)
  4. {
  5.         int menu, n, n_end, length;
  6.         char message[111];
  7.  
  8.         printf("\nHomework 4:\n\n");
  9.         printf("There are two parts: part 1 and part 2.\n");
  10.         printf("Enter the part number to execute (1 or 2):");
  11.         scanf("%d", &menu);
  12.         switch(menu){
  13.             case 1:
  14.                 printf("Part I:\n\n");
  15.                 printf("\nEnter a text message:");
  16.                 while(strlen(gets(message)) == 0);
  17.                 printf("\nYour message is:\n%s\nMessage Length: %d",message,strlen(message));
  18.                 /* Code to reverse message here */
  19.  
  20.                printf("\n\nYour message in reverse is:\n%s",message);
  21.                 printf("\n");
  22.                 break;
Case 2 will include some math stuff, which is why math.h is there and why the code is incomplete.

In any case, I'm not entirely sure how to make reverse work or why gets() isn't working for me. Did I screw something up somewhere?
Oct 12 '06 #1
1 3517
Banfa
9,065 Expert Mod 8TB
In any case, I'm not entirely sure how to make reverse work or why gets() isn't working for me. Did I screw something up somewhere?
No it is picking up the Enter you will have pressed after entering a menu option.

Try changing you scanf string to "%d\n"

or flushing stdin after you have done the scanf to clear input data with

rewind(stdin);

NOTE don't

fflush(stdin)

because the action of fflush is undefined for input streams.
Oct 13 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

20 posts views Thread by sahukar praveen | last post: by
52 posts views Thread by Christopher Benson-Manica | last post: by
45 posts views Thread by Rakesh | last post: by
24 posts views Thread by Sathyaish | last post: by
28 posts views Thread by rajendra.stalekar | last post: by
4 posts views Thread by kyle.tk | last post: by
47 posts views Thread by sudharsan | last post: by
10 posts views Thread by zahy[dot]bnaya[At]gmail[dot]com | last post: by
15 posts views Thread by rajash | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.