【C语言】字符函数和字符串函数

发布于:2024-03-28 ⋅ 阅读:(13) ⋅ 点赞:(0)

前言

在编程的过程中,我们经常要处理字符和字符串,为了⽅便操作字符和字符串,C语⾔标准库中提供了⼀系列库函数,接下来我们就学习⼀下这些函数。

一. 字符分类函数

C语⾔中有⼀系列的函数是专门做字符分类的,也就是⼀个字符是属于什么类型的字符的。
这些函数的使用都需要包含⼀个头⽂件是 <ctype.h>
<ctype.h>头文件中包含了一组字符分类函数,用于判断字符的类型。

常用的字符分类函数有:

1.isalpha:判断字符c是否为字母(a-z、A-Z)。

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

int main() {
    char c = 'A';

    if (isalpha(c)) {
        printf("%c是字母\n", c);
    } else {
        printf("%c不是字母\n", c);
    }

    return 0;
}

2.isdigit:判断字符c是否为数字(0-9)。

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

int main() {
    char c = '5';

    if (isdigit(c)) {
        printf("%c是数字\n", c);
    } else {
        printf("%c不是数字\n", c);
    }

    return 0;
}
 

3.isalnum:判断字符c是否为字母或数字。

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

int main() {
    char c = 'D';

    if (isalnum(c)) {
        printf("%c是字母或数字\n", c);
    } else {
        printf("%c既不是字母也不是数字\n", c);
    }

    return 0;
}
 

4.isspace:判断字符c是否为空白字符(空格、制表符、换行符等)。

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

int main() {
    char c = '\t';

    if (isspace(c)) {
        printf("%c是空白字符\n", c);
    } else {
        printf("%c不是空白字符\n", c);
    }

    return 0;
}
 

5.isupper:判断字符c是否为大写字母。

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

int main() {
    char c = 'F';

    if (isupper(c)) {
        printf("%c是大写字母\n", c);
    } else {
        printf("%c不是大写字母\n", c);
    }

    return 0;
}
 

6.islower:判断字符c是否为小写字母。

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

int main() {
    char c = 'd';

    if (islower(c)) {
        printf("%c是小写字母\n", c);
    } else {
        printf("%c不是小写字母\n", c);
    }

    return 0;
}
 

7.iscntrl:判断字符c是否为控制字符(ASCII码0-31和127)。

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

int main() {
    char c = '\n';

    if (iscntrl(c)) {
        printf("%c是控制字符\n", c);
    } else {
        printf("%c不是控制字符\n", c);
    }

    return 0;
}
 

8.ispunct:判断字符c是否为标点符号。

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

int main() {
    char c = '.';

    if (ispunct(c)) {
        printf("%c是标点符号\n", c);
    } else {
        printf("%c不是标点符号\n", c);
    }

    return 0;
}
 

9.isxdigit:判断字符c是否为十六进制数字(0-9、A-F、a-f)。

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

int main() {
    char c = 'C';

    if (isxdigit(c)) {
        printf("%c是十六进制数字\n", c);
    } else {
        printf("%c不是十六进制数字\n", c);
    }

    return 0;
}
 

10. isascii:判断字符c是否为ASCII字符(0-127)。

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

int main() {
   char c = '5';

   if (isascii(c)) {
       printf("%c是ASCII字符\n", c);
   } else {
       printf("%c不是ASCII字符\n", c);
   }

   return 0;
}

11. isblank:判断字符c是否为空白字符(空格或制表符)。

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

int main() {
   char c = ' ';

   if (isblank(c)) {
       printf("%c是空白字符\n", c);
   } else {
       printf("%c不是空白字符\n", c);
   }

   return 0;
}

除了这些常用的字符分类函数之外,还有一些其他的字符分类函数,如isgraphisprint等。

这些字符分类函数都返回一个非零值(真)代表满足条件,返回0(假)代表不满足条件。

二. 字符转换函数

<ctype.h>头文件中的字符转换函数用于将字符转换为其他形式的字符。

1. tolower:将字符c转换为小写字母形式。

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

int main() {
    char c = 'A';

    char lowercase = tolower(c);
    printf("%c转换为小写字母为:%c\n", c, lowercase);

    return 0;
}

2.toupper:将字符c转换为大写字母形式。

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

int main() {
    char c = 'b';

    char uppercase = toupper(c);
    printf("%c转换为大写字母为:%c\n", c, uppercase);

    return 0;
}

3. toascii:将字符c转换为对应的ASCII字符(0-127)。

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

int main() {
    char c = 'A';

    char ascii_char = toascii(c);
    printf("%c对应的ASCII字符为:%c\n", c, ascii_char);

    return 0;
}
 

这些字符转换函数可以帮助我们在程序中对字符进行转换操作,使得程序更加灵活和强大。

※ 如果文章对你有帮助的话,可以点赞收藏!!谢谢支持

本文含有隐藏内容,请 开通VIP 后查看