![]() |
VOOZH | about |
In C, iscntrl() is a predefined function used for string and character handling. ctype is the header file required for character functions. A control character is one that is not a printable character i.e, it does not occupy a printing position on a display.
This function is used to check if the argument contains any control characters. There are many types of control characters in C++ such as:
The iscntrl() function checks whether a character passed to the function as an argument is a control character or not. If the character passed is a control character, then the function returns a non-zero integer i.e. the ASCII value of the corresponding character. If not, it returns 0.
According to the standard ASCII character set, control characters are between ASCII codes 0x00 (NUL), 0x1f (US), and 0x7f (DEL). Specific compiler implementations may define additional control characters for many specific platforms in the extended character set (above 0x7f).
Syntax:
int iscntrl(int c);
Parameters:
c- This is the character to be checked.
Return Value:
This function returns a non-zero value i.e. the ASCII value of the character if c is a control character, else it returns 0.
Applications:
1. Given a string, print the string till the first control character is encountered.
Algorithm:
Examples:
Input: char str1[] = "GeeksforGeeks \t is good"; Output: geeksforgeeks Input: char str2[] = "Computer programming\n is best"; Output: Computer programming
geeksforgeeks is best
2. Given a string, traverse the string from the beginning and check if the character encountered is a control character or not.
Algorithm:
Examples:
Input: string1[]= "GeeksforGeeks \n is \n best platform \for Computer Science" Output: 3 Input: string2[]= "C is a \n powerful programming language." Output: 1
if R is passed to iscntrl() = 0 if is passed to iscntrl() = 2
3. Given a string, print ASCII values of all control characters.
Algorithm:
Examples:
Input:
for (i = 0; i <= 50; ++i)
{
if (iscntrl(i) != 0)
printf("%d ", i);
}
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Input:
for (i = 0; i <= 127; ++i)
{
if (iscntrl(i) != 0)
printf("%d ", i);
}
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 127
The ASCII value of all control characters are: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31