0

I'm doing a school project in C where I have to make a function that gets a string input and reverses the string as an output. I could use scanf("%s", answer);, but this only stores the first word of the string. Therefore I decided to go with gets(answer). I know this is unsafe because it doesn't allocate a specific memory size in answer, but I allready did this when defining the array: char answer[100];

I'm not interested in using fgets because the teachers will compile using Cygwin and this warning usually only shows up on Terminal when using a Mac:

warning: this program uses gets(), which is unsafe.

It will display this in the terminal right before prompting the user to type in a string. The other problem I have is that gets(answer) sometimes catches input from the printf("Type a string: ") above it. Is there any way to unvoid this?

Source code:

#include <stdio.h>
#include <string.h>

void reverseString();

int main() {

    char str[100];

    printf("Type your string here: ");
    gets(str);  /* Catching return from prinf so add one extra */
    gets(str);

    reverseString(str);
}

void reverseString(char string[]) {

    char temp;
    int startChar = 0;
    int endChar = strlen(string) - 1;

    while (startChar < endChar) {
        temp = string[startChar];
        string[startChar] = string[endChar];
        string[endChar] = temp;
        startChar++;
        endChar--;
    }
    printf("\nReverse string is: %s\n", string);
}

Running the code:

warning: this program uses gets(), which is unsafe.
Type your string here: hello world

Reverse string is: dlrow olleh

EDIT: So I tried using fgets(str, sizeof(str), stdin), but it still skips the user input part. I allso defined a buffer size for str like this: #define BUFFER_SIZE 100 and added it to the array, char str[BUFFER_SIZE].

EDIT: The apaerant reason why fgets(str, sizeof(str), stdin) isn't working, is because it catches the \n from stdin. I might have to flush it in one way or another.

Casper T.
  • 64
  • 7
  • 4
    `gets` is deprecated. Either use `fgets` or ignore the warning message being aware that an input that is too long may overrun your buffer and cause undefined behaviour. – Jabberwocky Sep 22 '16 at 15:57
  • _`gets(answer)` sometimes catches input from the `printf("Type a string: ")` above it_ : what does that mean ? Could you be more specific ? Maybe you should ask another specific question with a [mcve]. – Jabberwocky Sep 22 '16 at 15:59
  • 1
    Don't use `gets()` — it simply can't be used safely. See [Why `gets()` is so dangerous](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Sep 22 '16 at 16:00
  • @MichaelWalz The `gets(answer)` sometimes runs automaticly without the user actually inputting anything (gets is empty). If I add it twice, It runs normally, but sometimes it asks for two inputs.. – Casper T. Sep 22 '16 at 16:05
  • You teacher certainly wouldn't give you a bad grade because you are submitting a program using `fgets`instead of `gets`. If he does, he should be fired. – Jabberwocky Sep 22 '16 at 16:05
  • 1
    May I ask what the hesitation is to use `fgets`? I'm not sure I understand the Cygwin explanation, are you having some sort of problem with `fgets` in Cygwin? – Adrian Petrescu Sep 22 '16 at 16:06
  • @MichaelWalz: `gets` has been deprecated 17 years ago with C99. It is no part of the C standard since 5 years now (C11). – too honest for this site Sep 22 '16 at 16:06
  • @CasperT the 2 questions are not related, ask another question. That's how SO works. – Jabberwocky Sep 22 '16 at 16:06
  • @Olaf, I know, but the OP insisted and I just told him what he can do. – Jabberwocky Sep 22 '16 at 16:07
  • 1
    If your teacher is a bit sensitive to problematic code, you will fail the the homework for using `gets`. Your compiler clearly states it is unsafe and any compiler should warn. Worse: it is not part of the C standard. Simply **never ever use it**. – too honest for this site Sep 22 '16 at 16:08
  • @MichaelWalz: There is nothing to discuss about `gets` there is a safe alternative with `fgets`. If he intentionally insists writing unsafe code, we only can hope the teacher is strict. But I'm not very optimistic about that. – too honest for this site Sep 22 '16 at 16:13
  • @Olaf, well we've seen terrible teachers here. – Jabberwocky Sep 22 '16 at 16:29
  • "I'm not interested in using fgets ..." - that is plain ignorance! You intentionally write wrong code for some completely unrelated reason. Just because you don't get a warning does not mean your code is correct! – too honest for this site Sep 22 '16 at 16:32
  • The only time `gets()` will return immediately is if you use if after `scanf()` that doesn't consume the newline. See http://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf. – Barmar Sep 22 '16 at 16:38
  • @Olaf: Strictly, `gets()` was deprecated in TC3 published November 2007, so it is 'only' about 9 years so far. Still quite long enough. – Jonathan Leffler Sep 22 '16 at 16:55
  • The comment `/* Catching return from prinf so add one extra */` is peculiar. The `printf()` before it has no newline, but that wouldn't be material even if it did have one. The `printf()` function writes to standard output; the `gets()` function reads from standard input. While you could go through gyrations to get the standard output of a program connected to its input, you wouldn't be asking this question if that is what you'd done. You should also check the return value from your input functions. Maybe your first `gets()` succeeds, but the second detects EOF w/o overwriting `str`? – Jonathan Leffler Sep 22 '16 at 17:01
  • @JonathanLeffler: Thanks for the detail. Indeed I didn't check the original version, but only TC3. Anyway, OP was explicitly told by various users here what the problem is and not to use fgets. The excuse why not using `fgets` is at best weak. – too honest for this site Sep 22 '16 at 18:47

1 Answers1

-2

The question as I understand it is to give you a way to read in a string with spaces. However you don't want to use fgets() because of some reason I didn't follow. Here is another way to do so.

scanf ("%[^\n]%*c", str);
Brian Cross
  • 142
  • 1
  • 7