语言中scanf的问题 [英] problem in scanf in c langauge
问题描述
scanf(" \"%[^\"]",s);
printf("%s",s);
这需要在qoutes之内的输入
例如,如果我输入为"hello bye" go
那么它只会打印你好再见
我想知道的是scanf
中"\"%[^ \]"的含义和其他组合
还有一个疑问是%s用于字符串,为什么在这里不使用它而仍然需要字符串输入
this takes input which is within qoutes
for example if i give input as "hello bye " go
then it will print only hello bye
what i want to know is the meaning and other combination possible for " \"%[^\"]" in scanf
and one more doubt is that %s is used for string why it is not used here and still it takes string input
推荐答案
3分钟,发现google:
http://msdn.microsoft.com/en-us/library/xdb9w69d [ ^ ]
读取无界字符串
-------------------------------------------------- ------------------------------
要读取不由空格字符分隔的字符串,可以将方括号([])中的一组字符替换为s(字符串)类型的字符.方括号中的字符集称为控制字符串.读取相应的输入字段,直到不出现在控制字符串中的第一个字符.如果集合中的第一个字符是尖号(^),则效果相反:将读取输入字段,直到出现在其余字符集中的第一个字符.
请注意,%[a-z]和%[z-a]被解释为等同于%[abcde ... z].这是常见的scanf函数扩展,但请注意ANSI标准不需要它.
3 minutes with google found:
http://msdn.microsoft.com/en-us/library/xdb9w69d[^]
Reading Undelimited strings
--------------------------------------------------------------------------------
To read strings not delimited by whitespace characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The set of characters in brackets is referred to as a control string. The corresponding input field is read up to the first character that does not appear in the control string. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.
Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.
您在此处使用的格式代码实际上是在说:将第一个双引号之后的所有内容取为不包括第二个双引号".特殊的
The format code you have here is essentially saying "take everything after the first double quote up to but not including the second double quote". The special
%[]
结构仍然允许您指定一个字符串,但是您可以使用方括号内的代码来限制字符,而不是接受它看到的任何字符.在这种情况下,这些代码为
structure still allows you to specify a string, but instead of accepting any characters it sees, you can restrict the characters using codes within the square brackets. In this case those codes are
^\"
,其中前导插入符号的意思是后面没有任何字符",斜杠转义了双引号,因此结果是采用字符串,直到找到双引号". >
至于您可以执行的其他操作,请检查您特定库的文档,但是例如,您可以执行"az"之类的操作以仅接受小写字母(破折号指定字符集中a和z之间的所有字符) .因此,例如,如果您只想获取一个DNA序列,则可以使用
where the leading caret means "not any of the following characters", and the slash escapes the double quote, so the result is "take a string until you find a double quote".
As for what else you can do, check your particular library''s docs, but for instance you can do things like "a-z" to accept only lower case letters (the dash specifies all the characters between a and z in the character set). So for example if you wanted to take only a DNA sequence you could use
[AGCT]
,如果您不想区分大小写,则可以使用
, or
[AGCTagct]
.它类似于正则表达式,但非常简单.
希望有帮助!
if you didn''t want to be case sensitive. It is something like a regular expression, but very much simpler.
Hope that helps!
这篇关于语言中scanf的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!