Let’s take a look at what a scan set is in C language. A scan set is basically a specific symbol supported by the scanf family of functions. It is represented by %[]. In a scan set, we can only specify a character or a group of characters (case sensitive). When dealing with scan sets, the scanf() function can only process characters specified in the scan set.
#include<stdio.h> int main() { char str[50]; printf("Enter something: "); scanf("%[A-Z]s", str); printf("Given String: %s", str); }
Enter something: HElloWorld Given String: HE
It ignores characters written in lowercase letters. ‘W’ is also ignored because there are some lowercase letters before it.
Now, if the scan set has '^' in the first position, the specifier stops reading after the first occurrence of this character.
#include<stdio.h> int main() { char str[50]; printf("Enter something: "); scanf("%[^r]s", str); printf("Given String: %s", str); }
Enter something: HelloWorld Given String: HelloWo
Here, scanf() ignores the following characters after getting the letter 'r'. Using this feature, we can solve the problem of scanf not accepting strings with spaces. If we use %[^
] then it will get all characters till newline character is encountered.
#include<stdio.h> int main() { char str[50]; printf("Enter something: "); scanf("%[^</p><p>]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.
The above is the detailed content of In C language, scansets (Scansets). For more information, please follow other related articles on the PHP Chinese website!