How do you allow spaces to be entered using scanf?

前端 未结 11 1674
我寻月下人不归
我寻月下人不归 2020-11-21 06:05

Using the following code:

char *name = malloc(sizeof(char) + 256); 

printf(\"What is your name? \");
scanf(\"%s\", name);

printf(\"Hello %s. Nice to meet y         


        
相关标签:
11条回答
  • 2020-11-21 06:11

    People (and especially beginners) should never use scanf("%s") or gets() or any other functions that do not have buffer overflow protection, unless you know for certain that the input will always be of a specific format (and perhaps not even then).

    Remember than scanf stands for "scan formatted" and there's precious little less formatted than user-entered data. It's ideal if you have total control of the input data format but generally unsuitable for user input.

    Use fgets() (which has buffer overflow protection) to get your input into a string and sscanf() to evaluate it. Since you just want what the user entered without parsing, you don't really need sscanf() in this case anyway:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* Maximum name size + 1. */
    
    #define MAX_NAME_SZ 256
    
    int main(int argC, char *argV[]) {
        /* Allocate memory and check if okay. */
    
        char *name = malloc(MAX_NAME_SZ);
        if (name == NULL) {
            printf("No memory\n");
            return 1;
        }
    
        /* Ask user for name. */
    
        printf("What is your name? ");
    
        /* Get the name, with size limit. */
    
        fgets(name, MAX_NAME_SZ, stdin);
    
        /* Remove trailing newline, if there. */
    
        if ((strlen(name) > 0) && (name[strlen (name) - 1] == '\n'))
            name[strlen (name) - 1] = '\0';
    
        /* Say hello. */
    
        printf("Hello %s. Nice to meet you.\n", name);
    
        /* Free memory and exit. */
    
        free (name);
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 06:12

    You may use scanf for this purpose with a little trick. Actually, you should allow user input until user hits Enter (\n). This will consider every character, including space. Here is example:

    int main()
    {
      char string[100], c;
      int i;
      printf("Enter the string: ");
      scanf("%s", string);
      i = strlen(string);      // length of user input till first space
      do
      {
        scanf("%c", &c);
        string[i++] = c;       // reading characters after first space (including it)
      } while (c != '\n');     // until user hits Enter
      string[i - 1] = 0;       // string terminating
    return 0;
    }
    

    How this works? When user inputs characters from standard input, they will be stored in string variable until first blank space. After that, rest of entry will remain in input stream, and wait for next scanf. Next, we have a for loop that takes char by char from input stream (till \n) and apends them to end of string variable, thus forming a complete string same as user input from keyboard.

    Hope this will help someone!

    0 讨论(0)
  • 2020-11-21 06:17

    You can use this

    char name[20];
    scanf("%20[^\n]", name);
    

    Or this

    void getText(char *message, char *variable, int size){
        printf("\n %s: ", message);
        fgets(variable, sizeof(char) * size, stdin);
        sscanf(variable, "%[^\n]", variable);
    }
    
    char name[20];
    getText("Your name", name, 20);
    

    DEMO

    0 讨论(0)
  • 2020-11-21 06:18

    If someone is still looking, here's what worked for me - to read an arbitrary length of string including spaces.

    Thanks to many posters on the web for sharing this simple & elegant solution. If it works the credit goes to them but any errors are mine.

    char *name;
    scanf ("%m[^\n]s",&name);
    printf ("%s\n",name);
    
    0 讨论(0)
  • 2020-11-21 06:22

    You can use the fgets() function to read a string or use scanf("%[^\n]s",name); so string reading will terminate upon encountering a newline character.

    0 讨论(0)
  • 2020-11-21 06:25

    Try

    char str[11];
    scanf("%10[0-9a-zA-Z ]", str);
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题