- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Scansets in C
Let us see, what is the scanset in C. The scanset is basically a specifier supported by scanf family functions. It is represented by %[]. Inside scanset we can specify only one character or a set of characters (Case Sensitive). When the scanset is processed, the scanf() can process only those characters which are mentioned in the scanset.
Example
#include<stdio.h> int main() { char str[50]; printf("Enter something: "); scanf("%[A-Z]s", str); printf("Given String: %s", str); }
Output
Enter something: HElloWorld Given String: HE
It ignores the characters which are written in lowercase letters. The ‘W’ is also ignored because there are some lower case letters before it.
Now, if the scanset has ‘^’ in its first position, the specifier will stop reading after first occurrence of that character.
Example
#include<stdio.h> int main() { char str[50]; printf("Enter something: "); scanf("%[^r]s", str); printf("Given String: %s", str); }
Output
Enter something: HelloWorld Given String: HelloWo
Here the scanf() ignores the characters after getting the letter ‘r’. Using this feature, we can solve the problem that a scanf does not take strings with spaces. If we put %[^
], then it will take all of the characters until it gets a new line character.
Example
#include<stdio.h> int main() { char str[50]; printf("Enter something: "); scanf("%[^
]s", str); printf("Given String: %s", str); }
Output
Enter something: Hello World. This line has some spaces. Given String: Hello World. This line has some spaces.