小智 6
scanf只读取到第一个空格,就像cin >> someString. 假设您可以使用<iostream>and <string>,您想要的是
std::string str;
std::getline(std::cin, str);
Run Code Online (Sandbox Code Playgroud)
这将获取所有输入,直到用户按 Enter (\n)。
Use fgets() (which has buffer overflow protection) to get your input into a string.
printf("Enter a sentance: ");
fgets(st, 256, stdin);
printf("%s", st);
Run Code Online (Sandbox Code Playgroud)