示例#include<stdio.h>int main() { char str[50]; printf("enter something: "); scanf("%[a-z]s", str); printf("given string: %s", str);}
输出enter something: helloworldgiven string: he
它忽略了以小写字母书写的字符。‘w’也被忽略了,因为在它之前有一些小写字母。
现在,如果扫描集在第一个位置有‘^’,那么指定符在第一次出现该字符后停止读取。
示例#include<stdio.h>int main() { char str[50]; printf("enter something: "); scanf("%[^r]s", str); printf("given string: %s", str);}
输出enter something: helloworldgiven string: hellowo
在这里,scanf()在获取字母'r'后忽略了后面的字符。利用这个特性,我们可以解决scanf不接受带有空格的字符串的问题。如果我们使用%[^
],那么它将获取直到遇到换行字符为止的所有字符。
示例#include<stdio.h>int main() { char str[50]; printf("enter something: "); scanf("%[^
]s", str); printf("given string: %s", str);}
输出enter something: hello world. this line has some spaces.given string: hello world. this line has some spaces.
以上就是在c语言中,扫描集(scansets)的详细内容。
