473,491 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

3 New Member
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 3616
Banfa
9,065 Recognized Expert Moderator Expert
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

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

Similar topics

20
32984
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
52
2973
by: Christopher Benson-Manica | last post by:
gets() is universally acknowledged to be broken and useless; however, it is still part of the standard library. Why? Is there enough conforming code out there using gets() to justify retaining...
45
5263
by: Rakesh | last post by:
Hi, I have this function to reverse the given string. I am just curious if that is correct and there could be better way of doing it / probable bugs in the same. The function prototype is...
24
3229
by: Sathyaish | last post by:
This one question is asked modally in most Microsoft interviews. I started to contemplate various implementations for it. This was what I got. #include <stdio.h> #include <stdlib.h> #include...
28
4129
by: rajendra.stalekar | last post by:
Hi Folks!!! I have a string let's say "hi" and got to reverse it using just a single variable for swapping, how do I do it? Regards, Rajendra S.
4
2597
by: kyle.tk | last post by:
I am trying to make a function to reverse a string. What I have right now ends in a segfault. #include <string.h> #include <stdio.h> /* Reverse a string */ void strev(char *s){ char tmp;...
47
4952
by: sudharsan | last post by:
could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
10
6158
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi, I am trying to come up with a c style string reverser, I want it to take 1 argument Altough I would never do this in real life. Is there a way to do it? I wrote this function that fails : ...
15
6240
by: rajash | last post by:
Thanks for the additional comments. Here is a solution to an exercise I had problems with. I still don't think it's really what's wanted as it uses a "state variable" n - but I can't see how to...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.