2021年1月27日星期三

ANSI C: How do you ingest input from user and then print it in reverse?

I'm attempting to write a program (for class) that allows a user to type in a string and then outputs that string in reverse and it does this until the user types in "Done", "done", or "d".

Here is my current code:

#include <stdio.h>  #include <string.h>    #define BUFFER_SIZE 50    int main(void) {       char userText[BUFFER_SIZE];     int i;     int len;          do     {        fgets(userText, BUFFER_SIZE, stdin);        userText[len-1] = '\0';              len = strlen(userText);                   for ( i = len - 1; i >= 0; i-- )        {           printf("%c", userText[i]);        }        printf("\n");             } while ( ( strcmp(userText, "Done") != 0 ) && ( strcmp(userText, "done") != 0 ) && ( strcmp(userText, "d") != 0 ) );          return 0;  }  

As you can see, I used fgets because I have to allow the user to input a string that includes spaces and then I also cleared the buffer to avoid new lines. Here is the question I have to answer:

enter image description here

And here's my current output: enter image description here

https://stackoverflow.com/questions/65928715/ansi-c-how-do-you-ingest-input-from-user-and-then-print-it-in-reverse January 28, 2021 at 07:04AM

没有评论:

发表评论