- C++ Programs, GATE-CS-C-Language

Program and syntax for iscntrl(int c) function in C

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’EscapeAlert 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 programmingn is best”;
Output: Computer programmingC#include #include   int main () {  int i = 0, j = 0;  char str1[] = “geeksforgeeks t is good”;  char str2[] = “is best n point”;      while(!iscntrl(str1[i]))   {    putchar(str1[i]);    i++;  }      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: 1C#include #include   int main(){  char c;  int result;  c = ‘R’;  result = iscntrl(c);  printf(“if %c is passed to iscntrl() = %dn”,           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() = 23. 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