在 C 中使用 scanf() 函数处理空格 [英] Handling blank spaces with scanf() function in C

查看:54
本文介绍了在 C 中使用 scanf() 函数处理空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题.我想按以下格式从 scanf 扫描三个点:{[4;-1],[7;1.5],[4;4]}.我的代码如下所示:

I have following problem. I would like to scan three points from scanf in the following format: {[4;-1],[7;1.5],[4;4]}. My code looks like this:

scanf("{[%lf;%lf],[%lf;%lf],[%lf;%lf]}", &a1, &a2, &b1, &b2, &c1, &c2)

现在,我实际上正在测试这是正确的转换以进一步使用它.但我想允许用户输入这样的东西

Now, I am actually testing for this to be correct conversion to further work with it. But I would like to allow user to input something like this

<代码>{ [ 4 ;-1 ] , [ 7 ;1.5 ] , [ 4 ;4 ] }

甚至

    {  [ 


     4 ; 

     -1 ] ,  [ 7 ;                         1.5 ] , [   4 ; 4 


] } 

所以 scanf() 仍然能够给我 6 个正确的转换.我该怎么办?

so the scanf() still is able to give me 6 correct conversions. What should I do?

推荐答案

为了允许空格,在每个非空格之前有一个空格.

To allow for white-spaces, a white-space before each non-white-space.

// "{[%lf;%lf],[%lf;%lf],[%lf;%lf]}"
" { [%lf ;%lf ] , [%lf ;%lf ] , [%lf ;%lf ] }"

代码也可以在 "%lf" 之前添加一个空格,但这不会改变功能,因为该说明符已经消耗了可选的前导空格.

Code can also add a space before "%lf", but that does not change functionality as that specifier already consumes optional leading white-space.

" { [ %lf ; %lf ] , [ %lf ; %lf ] , [ %lf ; %lf ] }"

这篇关于在 C 中使用 scanf() 函数处理空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆