Program and syntax for iscntrl(int c) function in C
Last Updated :
31 May, 2021
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:
- Horizontal tab- ‘\t’
- Line feed- ‘\n’
- Backspace- ‘\b’
- Carriage return- ‘\r’
- Form feed- ‘\f’
- Escape
- Alert Character- '\a'
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:
- Traverse the given string character by character and write the characters to the standard output using putchar().
- Break the loop when a control character is found in the string.
- Print the final string from the standard output.
Examples:
Input: char str1[] = "GeeksforGeeks \t is good";
Output: geeksforgeeks
Input: char str2[] = "Computer programming\n is best";
Output: Computer programming
C
// C program to implement
// the above approach
#include <stdio.h>
#include <ctype.h>
// Driver code
int main ()
{
int i = 0, j = 0;
char str1[] = "geeksforgeeks \t is good";
char str2[] = "is best \n point";
// Prints string till control character \a
while(!iscntrl(str1[i]))
{
putchar(str1[i]);
i++;
}
// Prints string till control character \n
while(!iscntrl(str2[j]))
{
putchar(str2[j]);
j++;
}
return(0);
}
Outputgeeksforgeeks 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:
- Traverse the given string character by character up to its full length and check if the character is a control character.
- If it is a control character, increment the counter value by 1, else move to the next character.
- Print the value of the counter.
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
C
// C program to implement
// the above approach
#include <stdio.h>
#include <ctype.h>
// Driver code
int main()
{
char c;
int result;
c = 'R';
result = iscntrl(c);
printf("if %c is passed to iscntrl() = %d\n",
c, result);
c = '\n';
result = iscntrl(c);
printf("if %c is passed to iscntrl() = %d",
c, result);
return 0;
}
Outputif R is passed to iscntrl() = 0
if
is passed to iscntrl() = 2
3. Given a string, print ASCII values of all control characters.
Algorithm:
- Traverse the given string character by character up to its full length and check if the character is a control character.
- If it is a control character, print the ASCII value of the control character.
- Print till the value does not equal zero. (using for loop or while loop).
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
C
// C program to implement
// the above approach
#include <stdio.h>
#include <ctype.h>
// Driver code
int main()
{
int i;
printf("The ASCII value of all control characters are:\n");
for (i = 0; i <= 50; ++i)
{
if (iscntrl(i) != 0)
printf("%d ", i);
}
return 0;
}
OutputThe 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
Similar Reads
C Program For Char to Int Conversion Write a C program to convert the given numeric character to integer.Example:Input: '3'Output: 3Explanation: The character '3' is converted to the integer 3.Input: '9'Output: 9Explanation: The character '9' is converted to the integer 9.Different Methods to Convert the char to int in CThere are 3 mai
3 min read
Convert String to int in C In C, we cannot directly perform numeric operations on a string representing a numeric value. We first need to convert the string to the integer type. In this article, we will discuss different ways to convert the numeric string to integer in C language.Example:Input: "1234"Output: 1234Explanation:
6 min read
time() function in C The time() function is defined in time.h (ctime in C++) header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.Syntax: time_t time( time_t *seco
1 min read
sqrt() Function in C sqrt() function in C is a predefined function in the math.h library used to calculate the square root of a given number. To use this function, we must include the <math.h> header file in our program. It returns the square root of the number as a double and works with double data types, for oth
3 min read
How to Take Operator as Input in C? In C, an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. We may need to take operator as an input in some cases. In this article, we will learn how to take an operator as input in C. Get Input Operator in CTo take an operator as input in C, we
2 min read