📜  C 中 iscntrl(int c)函数的程序和语法

📅  最后修改于: 2022-05-13 01:57:39.528000             🧑  作者: Mango

C 中 iscntrl(int c)函数的程序和语法

在 C 中,iscntrl() 是用于字符串和字符处理的预定义函数。 ctype 是字符函数所需的头文件。控制字符是不可打印的字符,即它不占据显示器上的打印位置。

此函数用于检查参数是否包含任何控制字符。 C++中有许多类型的控制字符,例如:

  1. 水平制表符- '\t'
  2. 换行- '\n'
  3. 退格- '\b'
  4. 回车 - '\r'
  5. 换页- '\f'
  6. 逃脱
  7. 警报字符- '\a'

iscntrl()函数检查作为参数传递给函数的字符是否是控制字符。如果传递的字符是控制字符,则函数返回一个非零整数,即相应字符的 ASCII 值。如果不是,则返回 0。
根据标准 ASCII字符集,控制字符介于 ASCII 码 0x00 (NUL)、0x1f (US) 和 0x7f (DEL) 之间。特定的编译器实现可能会在扩展字符集中(高于 0x7f)为许多特定平台定义额外的控制字符。

句法:

int iscntrl(int c);

参数:
c-这是要检查的字符。

返回值:
如果 c 是控制字符,此函数返回一个非零值,即字符的 ASCII 值,否则返回 0。

应用:

1.给定一个字符串,打印字符串直到遇到第一个控制字符。

算法:

  1. 逐个字符遍历给定的字符串,并使用 putchar()字符字符标准输出。
  2. 在字符串中找到控制字符时中断循环。
  3. 从标准输出打印最终字符串。

例子:

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 
#include 
  
// 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);
}


C
// C program to implement
// the above approach
#include 
#include 
  
// 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;
}


C
// C program to implement
// the above approach
#include 
#include 
  
// 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;
}


输出
geeksforgeeks is best 

2.给定一个字符串,从头开始遍历字符串,检查遇到的字符是否是控制字符。

算法:

  1. 逐个字符遍历给定的字符串,直到字符全长,并检查该字符是否为控制字符。
  2. 如果是控制字符,则将计数器值加 1,否则移动到下一个字符。
  3. 打印计数器的值。

例子:

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 
#include 
  
// 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;
}
输出
if R is passed to iscntrl() = 0
if 
 is passed to iscntrl() = 2

3. 给定一个字符串,打印所有控制字符的 ASCII 值。

算法:

  1. 逐个字符遍历给定的字符串,直到字符全长,并检查该字符是否为控制字符。
  2. 如果是控制字符,则打印控制字符的 ASCII 值。
  3. 打印直到值不等于零。 (使用 for 循环或 while 循环)。

例子:

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 
#include 
  
// 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;
}
输出
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