How to save blank characters in user input in C language

How to save blank characters in user input in C language

In C language, scanf and fgets functions are usually used to obtain the data input from the keyboard.

If scanf is used, the following modes can be used:

"%[^\n]%*c"
So‮ecru‬:www.lautturi.com

example:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[]) {

    char name[20];

    printf("Enter a name : ");
    scanf("%[^\n]%*c", &name);

    printf("the name is: %s\n", name);

    return 0;

}

example of output:

Enter a name : lautturift idea
the name is: lautturift idea

We can also use fgets to obtain data from files or pipes.
So we just need to set stdin input from the standard input. The standard input is generally keyboard, mouse, etc.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[]) {

    char name[20];

    printf("Enter a name : ");
    fgets(name, 20, stdin);

    printf("the name is: %s\n", name);

    return 0;

}

output:

Enter a name : hello wolrd
the name is: hello wolrd
Created Time:2017-08-29 09:06:17  Author:lautturi