Edit Set

EditSet:

This will be great useful in strings.You can specify the characters you want to include or exclude in C.This will work with scanf.
Include set:

Ex:scanf(“%[a-z]“,a);
means it will read all the characters between a to z and if other character in encountered, it will stop reading the variable.
Ex:if the input is adithyaU, then the value of string variable ‘a’ will be adithya only.

You can specify the individual chars also
Ex:scanf(“%[a,d,i]“,a);
will read only the chars a,d,i
if input adithya then the value of ‘a’ will be adi only.

Special characters and white spaces may also be included in the edit set

Ex:scanf(“%[*],a);
will read only chars *

Exclude set

ex:scanf(“%[^t]“,a);
means it wil read all the characters until t is encountered

Here all the chars means special chars and white space chars are also included.That means draw back of scanf over gets can be overcome by using exclude set

if i input adithya then the value of ‘a’ will be adi

ex:scanf(“%[^A-Z]“,a);
means no Capital’s are allowed

ex:scanf(“%[^\n]“,a);
read ALL chars until enter key is encountered(Function as gets())

The use of edit set is illustrated in the following program:

Write a c program which makes scanf to accept the special as well as white space chars

 

Example Program:

#include<stdio.h>
void main()
{
char str[100];
printf(“Enter any string\n”);
scanf(“%[^\n]s”,str); //<—– Imp step——–
printf(“\n You entered string %s \n”,str);
}


You may also like

Find out what I am doing by following me on Twitter... Tweet This Post!

Leave a Reply

Comment on the next level:

- Enter your info on the left

- Create an avatar with Gravatar!