怎样使用C语言的scanf函数格式化输入带空格的字符串

怎样使用C语言的scanf函数格式化输入带空格的字符串

  1. scanf(“%s”,a);当遇到空格就自动截断了,怎样才能输入带空格的字符串呢?
(1)用gets()函数
(2)利用格式符“%[]”它的作用为扫描字符集合。
Scanf(“%[^c]”,str);
其中“c”是一个具体的字符常量(包括控制字符)。
当输入字符串时,字符“c”将被当作当前输入的结束符。
如同默认情况下空格和回车键以及TAB键是作为字符串输入的结束符一样,利用此格式符便相当于由编程者自己指定一个输入结束符。
char a[100];
scanf(“%[^\n]”,a);
printf(“%s\n”,a);
注意:如果有多个scanf函数,上一个scanf结束会有一个\n,若是下一个scanf有正则表达式中[^\n],遇到\n直接退出了,所以没有输出s3。会很奇怪。
代码如下:
#include<stdio.h>
#include “conio.h”
int main()
{
       printf(“Please input a word,string,scentence in trun:\n”);
       char s1;
       char s2[20];
       char s3[30];
       scanf(“%c”, &s1);
       scanf(“%s”, s2);
       //system(“cls”);//清屏函数
       scanf(“%[^\n]”, s3);
       //scanf(“%[^A]”, s3);
       printf(“this is your output:\n”);
       printf(“%c\n”, s1);
       printf(“%s”, s2);
       printf(“%s\n”, s3);
       system(“pause”);
       return 0;
}
解决方案:这是不要用^\n作为正则表达式中的内容了,可以用其他字符,比如上面代码中注释行中的^A。结果就正确了。
FutureTech

8条评论

Regina 发布于3:15 下午 - 10月 14, 2018

Hey there! I simply would like to give yoou a bbig thumbs up for the
excellent information you have got right ere oon this post.
I am coming back to your website for morde soon. http://oxford-dictionaries.net/__media__/js/netsoltrademark.php?d=teaching.bioinformatics.dtu.dk

Regina 发布于3:15 下午 - 10月 14, 2018

Hey there! I simply would like too give you a big thumbs up for the excellent information you have gott rioght here on this post.
I am coming back to your website for more soon. http://oxford-dictionaries.net/__media__/js/netsoltrademark.php?d=teaching.bioinformatics.dtu.dk

scr888 test 发布于4:26 下午 - 10月 14, 2018

Kim opted with Associated Content in March of 2007.
No easily 48 words should use here in as many different collocations possible.

Most girls love animals and a family pet can be a great theme. http://selmark.net/__media__/js/netsoltrademark.php?d=casino-fair.com%2F918kiss-best-platform-for-casino-games%2F

lpe88 apk download 发布于8:17 下午 - 10月 15, 2018

Going guide much specially for competitive keywords. Determining the true
quantity of incoming links for a blog doesnt seem possible particularly.
Do a search for tiny silk flowers to include in your presentation. https://r3e.de/918kissjinqianwa536825

lpe88 apk download 发布于8:17 下午 - 10月 15, 2018

Going guide much specially for competitive keywords. Determining
the true quantity of incoming links for a blog doesnt seem possible particularly.
Do a search for tiny silk flowers to include in your
presentation. https://r3e.de/918kissjinqianwa536825

3win8 发布于9:21 上午 - 10月 16, 2018

Oh my goodness! Awesome article dude! Many thanks, However I am experiencing problems with your RSS.
I don’t understand why I cannot join it. Is there anybody having identical RSS
problems? Anybody who knows the solution can you kindly respond?
Thanks!! https://918.network/casino-games/70-3win8

3win8 发布于9:21 上午 - 10月 16, 2018

Oh my goodness! Awesome article dude! Many thanks, However I am experiencing problems with your RSS.
I don’t understand why I cannot join it. Is there anybody having
identical RSS problems? Anybody who knows the solution can you kindly respond?
Thanks!! https://918.network/casino-games/70-3win8

Trista 发布于1:46 上午 - 11月 17, 2018

Way cool! Some very valid points! I appreciate you penning
this post plus the rest of the site is very good. I truly love your blog..
Great colors & theme. Did you make this web site yourself?
Please reply back as I’m planning to create my own personal website and would like to know
where you got this from or exactly what the theme
is named. Appreciate it! I have been browsing online more than 3 hours today, yet I never found any interesting article like yours.
It’s pretty worth enough for me. In my view, if all site owners and
bloggers made good content as you did, the web will be much more useful than ever before.
http://foxnews.co.uk