scansets in C
In this article, we will discuss the scansets in C with its syntax and example.
In C programming, a "scanset" is a useful feature provided by the scanf() function. It enables the reading of formatted input from various input streams, such as the standard input (keyboard). With scansets, we can define a specific group of characters that scanf() should recognize as valid input for a particular format specifier.
By utilizing scansets in scanf(), programmers can specify a set of characters that are allowed for input, and any character not belonging to this set will signal the termination of the scanset. This capability empowers developers to read and process specific patterns of characters, making interactive user input handling more effective and versatile in C programs.
Suppose you are reading a storybook, and there is a special page where you can only find certain types of characters, like letters, numbers, or even spaces. Its special page is like a "scanset". Instead of reading the entire book, you can quickly find and pick out just the characters you are interested in.
In terms of C programming, when we talk about "scansets", it's like we have a magical pair of glasses that can only see specific types of characters from what a user writes on a piece of paper or types on a computer.
Example:
If we use %[A-Za-z], it means we want to find only uppercase and lowercase letters (no numbers or symbols).
If we use %[0-9], it means we want to find only numbers (no letters or symbols).
If we use %[A-Za-z0-9], it means we want to find both uppercase and lowercase letters and numbers (no symbols).
By using scansets, we can instruct the computer to read only the characters we are interested in and ignore the rest.
Syntax for the Scansets in C
It has the following syntax:
scanf("%[set_of_characters]",variable_name);
In the format specifier %[set_of_characters]:
% indicates the start of the format specifier.
[ indicates the start of the scanset.
set_of_characters specifies the set of characters that are allowed as input. Any character not present in this set will terminate the scansets.
Example:
Let's take an example to demonstrate the scansets in C language.
Code:
#include <stdio.h>
int main() {
char uppercase[20];
char lowercase[20];
char alphanumeric[30];
int num;
char sentence[100];
char str[50];
char no_digits[50];
char input[15];
printf("Enter a sequence of uppercase letters: ");
scanf("%[A-Z]", uppercase);
while (getchar() != '\n'); // Clear the input buffer
printf("Uppercase: %s\n", uppercase);
printf("Enter a sequence of lowercase letters: ");
scanf("%[a-z]", lowercase);
while (getchar() != '\n'); // Clear the input buffer
printf("Lowercase: %s\n", lowercase);
printf("Enter a sequence of alphanumeric characters: ");
scanf("%[A-Za-z0-9]", alphanumeric);
while (getchar() != '\n'); // Clear the input buffer
printf("Alphanumeric: %s\n", alphanumeric);
printf("Enter a sequence of digits: ");
scanf("%d", &num);
while (getchar() != '\n'); // Clear the input buffer
printf("Number: %d\n", num);
printf("Enter a sentence with spaces: ");
scanf(" %[^\n]", sentence);
while (getchar() != '\n'); // Clear the input buffer
printf("Sentence: %s\n", sentence);
printf("Enter a string until a comma is encountered: ");
scanf(" %[^\n]", str);
while (getchar() != '\n'); // Clear the input buffer
printf("String: %s\n", str);
printf("Enter a string excluding digits: ");
scanf(" %[^0-9]", no_digits);
while (getchar() != '\n'); // Clear the input buffer
printf("String without digits: %s\n", no_digits);
printf("Enter a string with a maximum length of 14 characters: ");
scanf(" %14s", input);
while (getchar() != '\n'); // Clear the input buffer
printf("Input: %s\n", input);
return 0;
}
Output:
![scansets in C](https://static.tutorialandexample.com/c/scansets-in-c1.png)
Explanation:
The program demonstrates the use of scansets with the scanf() function to read specific patterns of characters from the user's input. The program asks the user to enter various sequences of characters and digits and then uses scanf() with appropriate scansets to capture and process the input accordingly.
After that, the program begins by declaring arrays to store the input for each specific case: uppercase to store uppercase letters, lowercase to store lowercase letters, alphanumeric to store alphanumeric characters, num to store a numeric value, sentence to store a sentence with spaces, str to store a string until a comma is encountered, no_digits to store a string excluding digits, and input to store a string with a maximum length of 14 characters.
For each type of input, the program uses scanf() with the appropriate scansets to read the data from the user. After reading each input, the program clears the input buffer to avoid interference with subsequent scanf() calls using while (getchar() != '\n').
Finally, the program prints the results of each input, displaying the extracted strings and numbers.
In summary, the program illustrates the versatility of scansets in scanf() to read specific character patterns and numeric values from the user's input. It demonstrates how to use scansets to store uppercase and lowercase letters, alphanumeric characters, numeric values, strings with spaces or until a comma, and strings excluding digits. Additionally, it ensures proper clearing of the input buffer to avoid issues with consecutive scanf() calls.