Home  >  Article  >  Backend Development  >  In C language, scansets (Scansets)

In C language, scansets (Scansets)

PHPz
PHPzforward
2023-09-08 23:21:031315browse

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);
}

Output

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.

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, 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);
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Bit fields in CNext article:Bit fields in C