Scanset In C

For example:

Example

scanf(%s[A-Z,_,a,b,c]s,str);

It will analyze each character within the scanset provided. Let's examine the scanset with an illustration. In the given instance, the character array 'str' is designated to store solely uppercase letters; any other characters will be excluded from it.

Example 1:

Character matching with specifics:

Let's suppose you want to accept only a single numeric input ranging from 0 to 9. In this case, you would use a scanset of %[0-9]. Here is a visual representation:

Example

#include <stdio.h>

int main() {
    char input[100];
printf("Enter a number: ");
scanf("%[0-9]", input);
printf("You entered: %s\n", input);
    return 0;
}

Output:

Output

Enter a number: 12345
You entered: 12345

Example 2:

Matching alphabetic characters:

You can utilize %[a-zA-Z] to scan a string that exclusively consists of alphabetical characters (from a to z and A to Z). Here's an example:

Example

#include <stdio.h>

int main() {
    char input[100];
printf("Enter a word: ");
scanf("%[a-zA-Z]", input);
printf("You entered: %s\n", input);
    return 0;
}

Output:

Output

Enter a word: logic practice
You entered: logic practice

Example 3:

Matching a set of characters precisely:

Within the scanset, you have the ability to specify multiple characters that you want to match. For example, you could utilize %[0-9A-Z] to scan and extract a sequence that comprises solely of capital letters and numbers. Here is a visual representation:

Example

#include <stdio.h>

int main() {
    char input[100];
printf("Enter a string (digits and uppercase letters only): ");
scanf("%[0-9A-Z]", input);
printf("You entered: %s\n", input);
    return 0;
}

Output:

Output

Enter a string (digits and uppercase letters only): HELLO1234
You entered: HELLO1234

Example 4:

Characters are matched until a specific character is encountered.

You have the option to employ the caret symbol at the beginning of the scanset to invert the matching behavior. This will enable the program to scan continuously until it encounters the specified character. For example, you can utilize %[^\n] to scan a string until a newline character is reached.

Example

#include <stdio.h>

int main() {
    char input[100];
printf("Enter a sentence (ends with Enter key): ");
scanf("%[^'\n']", input);
printf("You entered: %s\n", input);
    return 0;
}

Output:

Output

Enter a sentence (ends with Enter key): Hello world!
You entered: Hello world!

Example 5:

Let's apply the scanset to develop the gets function. The gets function retrieves input from the standard input (stdin) and stores it in the buffer specified by scanset until it encounters a newline character or reaches the end of the file (EOF).

Example

#include <stdio.h>

int main(void)
{
	char str[128];

	printf("Enter a string with spaces: ");
	scanf("%[^\n]s", str);

	printf("You entered: %s\n", str);

	return 0;
}

Output:

Output

Enter a string with spaces: hello world
You entered: hello world

Note: Never use the gets method. Gets will keep storing characters after the end of the buffer, making it hard to predict how many characters it will read without knowing the data in advance. It has been utilized to circumvent computer security. In its place, use the fgets function .

Input Required

This code uses input(). Please provide values below: