scanf() - To Get Special Charecters and To Use it based on Constraints
As we know already C is one of the most important languages
which maximize all other languages by giving its scalability.
People might have commonly known about scanf
and printf. Apart from that there are some cases where special characters
should be processed through C program.
In order to process the special characters, we
should be aware of the methods that can get the special characters as input
from the users.
In C, you can give any special character as
input by utilizing the below functions:
Functions:
1. In such situation like where you should get only the specified range of
inputs from the user you can
use the below.
This can be done by the regular
expressions such as below
Ex,
scanf("\n%[abc]",name);
2. In such situations, you are not supposed to get some specified
range of inputs from the
users. In those cases use the regular expression as below
Ex,
scanf("\n%[^abc]",inp4);
-> If you give like this, you can't give a
or b or c as input.
3. In some cases where you shoud read all the special
characters util you find some
particular text or particular special character in the input entered by the
users. Use the below regular expression.
Ex,
scanf("\n%[^]]",inp3);
Example Program:
#include<stdio.h>
main()
{
char
inp1[20],inp2[20],inp3[20],inp4[20],inp5[20],inp6[20];
printf("\n Enter Any String or
Dotted string\n");
scanf("%s",inp1);
printf("\n
Entered First String is:%s\n",inp1);
printf("\n
Enter any string that contains only a or b or c");
scanf("\n%[abc]",inp2);
printf("\n
Entered String is:%s",inp2);
printf("\n Enter string to read up to ] symbol");
scanf("\n%[^]]",inp3);
printf("\n Enter
string and this scanf will read input from user until it finds any ]
%s",inp3);
printf("\n Enter string to read up to a or b or c");
scanf("\n%[^abc]",inp4);
printf("\n Entered First
String is:%s",inp4);
}
OUTPUT:




Comments