read user input string with space between quotes in C and keep the quotes

200 Views Asked by At

e.g "task 1"

What character class can I use in order to keep the quotes?

1

There are 1 best solutions below

0
On

In order to do what your intending, You need to use scanf and have a predetermined amount of characters you want between the string. This will construct a string the keeps literal quotes and a null character at the end, so the string can be reprinted.

#include <stdio.h>

int main(void) 
{
    int charsToRead = 6;
    char inputString[9];
    char* inputPointer = inputString + 1;
    inputString[0] = '"';
    inputString[7] = '"';
    inputString[8] = '\0';
    printf("type something with quotes\n");
    scanf("\"%6c\"", inputPointer);
    printf("the captured string is %s", inputString);
    return 0;
}

to take in the string "task 1", you need 6 characters to be read between the quotes, which is reflected above.