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.
 
    