Scansets In C

Scansets In C

Scanset specifiers are used to specify a set of characters that Scanf should match and store in a variable. In C’s scanf family of functions, scanset specifiers are denoted by %[]. Inside a scanset, you can specify either single characters or a range of characters that you want to match and store. It’s important to note that scansets are case-sensitive, meaning uppercase and lowercase characters are treated as distinct. You can define a scanset by placing characters inside square brackets. You can also use a comma to separate individual characters or character ranges within the scanset.

Simple Scanset Example

The below example will store only capital letters to the character array ‘str’, any other character will not be stored inside the character array.

/* A simple scanset example */
#include <stdio.h>
 
int main(void)
{
    char str[128];
 
    printf("Enter a string: ");
    scanf("%[A-Z]s", str);
 
    printf("You entered: %s\n", str);
 
    return 0;
}

Output

[root@centos-6 C]# ./scan-set 
  Enter a string: Skill Vertex
  You entered: Skill Vertex

Another scanset example with ^

In C programming, when you use scanset specifiers in functions like scanf, you can put characters inside square brackets to specify a set of characters you want to read. If you start with a ‘^’ inside the brackets, it means you want to read characters that are NOT in the specified set. The scanning will stop as soon as it finds a character that is part of the specified set. For example, if you specify “%[^o]s” and input “Skill Vertex,” it will read and store characters until it finds the letter ‘o,’ and then it stops. So, in this case, it will store “Skill” in your variable.

scanf("%[^o]s", str);

Look at the example given below:

/* Another scanset example with ^ */
#include <stdio.h>
 
int main(void)
{
    char str[128];
 
    printf("Enter a string: ");
    scanf("%[^o]s", str);
 
    printf("You entered: %s\n", str);
 
    return 0;
}

Output

 [root@centos-6 C]# ./scan-set 
  Enter a string: http://Skill Vertex
  You entered: http://Skill f
  [root@centos-6 C]# 

Implementation of gets() function using scanset

You can create a simple gets() function using scanf with a scanset to read a line from stdin. Here’s a basic implementation in C:

/* implementation of gets() function using scanset */
#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

[root@centos-6 C]# ./gets 
  Enter a string with spaces: Skill Vertex
  You entered: Skill Vertex
  [root@centos-6 C]# 

FAQ- Scansets In C

Q1. What is the Scanset function in C?

Ans. You can make a scanset by putting characters inside square brackets in C. It’s important to remember that scansets are case-sensitive, so uppercase and lowercase letters are treated differently. You can also use a comma to separate the characters you want to include in the scanset. Here’s an example:
For instance, scanf("%s[A-Z,_,a,b,c]s", str); will scan and store characters that match the set [A-Z,_,a,b,c] in the variable str

Q2. What is the syntax of scanf char in C?

Ans. The general syntax of scanf – int scanf(const char *format, Object *arg(s))

Q3. How to scan a string in C?

Ans. In C, the scanf function with the “%s” format specifier stops reading a string at the first space. To accept a string with spaces in user input, you can use one of the following four common methods

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment