C 语言中的 isspace

C 语言中的 `isspace` 函数是该编程语言中提供的标准库函数之一,它用于检查给定的字符是否为空格。 `isspace` 的基本语法如下所示。

此编程语言中的 `isspace` 函数接受一个字符作为参数,并判断给定的字符是否为空格。

isspace(char)

C 语言 `isspace` 示例

`isspace` 函数用于判断给定的字符是否为空格字符。

#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch = ' ';
    
    if(isspace(ch))
    {
        printf("The Character ch is a Space Character \n");
    }
    else
    {
        printf("The Character ch is Not a Space Character \n");
    }
    return 0;
}
The Character ch is a Space Character 

`isspace` 示例 2

程序允许用户输入任何字符,并使用 `isspace` 函数检查该字符是否为空格。

#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch;
    printf("Please Enter Any Valid Character : \n");
    scanf("%c", &ch);

    if(isspace(ch))
    {
      printf("\n You have entered a Space Character");         
    }
    else
    {
      printf("\n %c is not a Space Character", ch);
      printf("\n I request you to enter Space Character");	
    }
}
isspace Example

我输入一个大写字母。

Please Enter Any Valid Character : 
T

 T is not a Space Character
 I request you to enter Space Character

首先,我们声明了一个名为 `ch` 的字符变量。

char ch;

第一个C 语言语句会提示用户输入任何字符。然后,我们使用 `scanf` 将用户输入的字符赋给 `ch` 变量。

printf("Please Enter Any Valid Character : \n");
scanf("%c", &ch);

接下来,我们使用If 语句,通过 `isspace` 函数检查字符是否为空格。如果条件为 True,则会打印以下语句。

printf("\n You have entered a Space Character");

如果上述条件为 FALSE,则表示给定的字符不是空格。因此,它会打印下面的语句。

printf("\n %c is not a Space Character", ch);
printf("\n I request you to enter Space Character");

不使用 `isspace` 的 C 语言示例

此程序允许用户输入任何字符,并使用ASCII 表检查该字符是否为空格。这里我们不使用内置函数 `isspace`。

#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch;
    printf("Please Enter Any Valid Character : \n");
    scanf("%c", &ch);

    if(ch == 32)
    {
      printf("\n You have entered a Space Character");         
    }
    else
    {
      printf("\n %c is not a Space Character", ch);
      printf("\n I request you to enter Space Character");	
    }
}
Please Enter Any Valid Character : 
 

 You have entered a Space Character

由于 32 是空格的 ASCII 值,我们用 32 替换了 `isspace` 函数。我建议您仔细检查 ASCII 表。让我尝试另一个值。

Please Enter Any Valid Character : 
m

 m is not a Space Character
 I request you to enter Space Character