Home > Backend Development > C++ > body text

In C language, scansets (Scansets)

PHPz
Release: 2023-09-08 23:21:03
forward
1379 people have browsed it

In C language, scansets (Scansets)

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.

Example

#include<stdio.h>
int main() {
   char str[50];
   printf("Enter something: ");
   scanf("%[A-Z]s", str);
   printf("Given String: %s", str);
}
Copy after login

Output

Enter something: HElloWorld
Given String: HE
Copy after login

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.

Example

#include<stdio.h>
int main() {
   char str[50];
   printf("Enter something: ");
   scanf("%[^r]s", str);
   printf("Given String: %s", str);
}
Copy after login

Output

Enter something: HelloWorld
Given String: HelloWo
Copy after login

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.

Example

#include<stdio.h>
int main() {
   char str[50];
   printf("Enter something: ");
   scanf("%[^</p><p>]s", str);
   printf("Given String: %s", str);
}
Copy after login

Output

Enter something: Hello World. This line has some spaces.
Given String: Hello World. This line has some spaces.
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template