C语言库函数

声明: 本篇博客的学习途径主要为以下网站和课堂讲解,发博客目的仅为学习使用,在该博客的基础上做了一定程序的简略和修改。
参考博客

字符串 库函数 (头文件:string.h)

strcpy() 字符串拷贝函数

头文件:

头文件:string.h

语法原型:

char* strcpy(char* strDestination, const char* strSource);

参数说明:

  • strDestination:目的字符串。
  • strSource:源字符串。

【功能】对字符串进行复制(拷贝):strcpy() 会把 strSource 指向的字符串复制到 strDestination。

【注意事项】必须保证 strDestination 足够大,能够容纳下 strSource,否则会导致溢出错误。

【返回值】目的字符串 strDestination


【实例】使用C语言 strcpy() 函数将字符串 src 复制到 dest。

#include <stdio.h>
#include <string.h>
int main(){
    char dest[50] = { 0 };
    char src[50] = { "http://c.biancheng.net" };
    strcpy(dest, src);
    puts(dest);
    return 0;
}
运行结果:
http://c.biancheng.net

strlen() 求字符串长度

头文件:

头文件:string.h

语法原型:

size_t strlen(const char* str);

参数说明:

  • 参数 str 表示要求长度的字符串。

【功能】从字符串的开头位置依次向后计数,直到遇见\0,然后返回计时器的值。

【返回值】返回值:字符串 str 的长度。
【注意事项】最终统计的字符串长度不包括\0。


【实例】使用C语言 strlen() 函数求用户输入的字符串的长度。

#include <stdio.h>
#include <string.h>
int main(){
    char str[100] = { 0 };
    size_t len;
    gets(str);
    len = strlen(str);
    printf("Length: %d\n", len);
    return 0;
}
Java C++ Linux Python Shell: http://c.biancheng.net↙
Length: 51

strcmp() 比较两个字符串(找不同,比较不同的第一个字母的ASCII码大小)

strcmp() 函数用于对两个字符串进行比较(区分大小写)。

头文件:

头文件:string.h

语法原型:

int strcmp(const char* stri1,const char* str2);

参数说明:

  • 参数 str1 和 str2 是参与比较的两个字符串。

【功能】strcmp() 会根据 ASCII 编码依次比较 str1 和 str2 的每一个字符,直到出现不到的字符,或者到达字符串末尾(遇见\0)。
【返回值】

  • 如果返回值 < 0,则表示 str1 小于 str2。
  • 如果返回值 > 0,则表示 str2 小于 str1。
  • 如果返回值 = 0,则表示 str1 等于 str2。

【注意事项】字符串进行比较(区分大小写)


【实例】使用C语言 strcmp() 函数比较用户输入的两个字符串。当两次输入一样时退出

#include <stdio.h>
#include <string.h>
int main(){
    char str1[50] = { 0 };
    char str2[50] = { 0 };
    int i = 1;
    do {
        printf("******第%d次输入******\n", i);
        gets(str1);
        gets(str2);
        i++;
    } while ( strcmp(str1, str2) );
    return 0;
}
******第1次输入******
123abc↙
456edf↙
******第2次输入******
Java Linux C++ Python C# MySQL↙
java linux c++ python c# mysql↙
******第3次输入******
Golang is great!↙
Golang is great!↙

strchr() 在字符串中查找某个字符,返回其位置

头文件:

头文件:string.h

语法原型:

char* strchr(const char* str, int c);

参数说明:

  • str:被查找的字符串。
  • c:要查找的字符。

【功能】
strchr() 函数会依次检索字符串 str 中的每一个字符,直到遇见字符 c,或者到达字符串末尾(遇见\0)。

【返回值】

  • 返回在字符串 str 中第一次出现字符 c 的位置
  • 如果未找到该字符 c 则返回 NULL。

【注意事项】字符串进行比较(区分大小写)


【实例】使用C语言 strchr() 函数在C语言中文网的网址中查找g字符。

#include <stdio.h>
#include <string.h>
int main(){
    const char *str = "http://c.biancheng.net/";
    int c = 'g';
    char *p = strchr(str, c);
    if (p) {
        puts("Found");
    } else {
        puts("Not found");
    }
    return 0;
}
Found

strcspn()在字符串中寻找列表中有的第一个值,返回其位置

头文件:

头文件:string.h

语法原型:

size_t strcspn(const char* str, const char* reject);

参数说明:

  • str:要检索的字符串。
  • reject:该字符串包含了要在 str 中进行匹配的字符列表。

【功能】
strchr() 函数会依次检索字符串 str 中的每一个字符,直到遇见字符 c,或者到达字符串末尾(遇见\0)。

【返回值】
str 中第一次出现 reject列表中 字符的位置。

【注意事项】字符串进行比较(区分大小写)


【实例】演示C语言 strcspn() 函数的用法。

#include <stdio.h>
#include <string.h>
int main(){
    char str[50] = { "http://c.biancheng.net" };
    char keys[50] = { "?.,:\"\'-!" };
    int i = strcspn(str, keys);
    printf("The firsr punctuation in str is at position %d.\n", i);
    return 0;
}
The firsr punctuation in str is at position 4.

解释:
第一次找到的是属于reject列表的字符是:出现在str中的第四位


strcat() 字符串拼接

头文件:

头文件:string.h

语法原型:

char*strcat(char* strDestination, const char* strSource);

参数说明:

  • strDestination:目的字符串;
  • strSource:源字符串。

【功能】
strcat() 函数把 strSource 所指向的字符串追加到 strDestination 所指向的字符串的结尾

【返回值】
指向 strDestination 的指针。

【注意事项】

  • 必须要保证 strDestination 有足够的内存空间来容纳两个字符串,否则会导致溢出错误。
  • strDestination 末尾的\0会被覆盖,strSource 末尾的\0会一起被复制过去,最终的字符串只有一个\0。

【实例】使用C语言 strcat() 函数将用户输入的两个字符串拼接在一起。

#include <stdio.h>
#include <string.h>
int main(){
    char str1[101] = { 0 };
    char str2[50] = { 0 };
    gets(str1);
    gets(str2);
    strcat(str1, str2);
    puts(str1);
    return 0;
}
C++ Python Linux Java Shell Qt C# Servlet↙
http://c.biancheng.net↙
C++ Python Linux Java Shell Qt C# Servlethttp://c.biancheng.net

字符 库函数 (头文件:ctype.h)

isalpha()函数:一个字符?字母

头文件:

头文件:ctype.h

语法原型:

int isalpha(int c);

参数说明:
参数 c 表示要检测字符或者 ASCII 码。

【返回值】

  • 返回值为非 0(真)表示 c 是字母
  • 返回值为 0(假)表示 c 不是字母。

【实例】C语言 isalpha() 函数判断一个字符串中的字符是否是字母。

#include <stdio.h>
#include <ctype.h>
int main()
{
    int i = 0;
    char str[] = "C++ Java C#";
    while (str[i])
    {
        if (isalpha(str[i])) {
            printf("%c is alphabetic\n", str[i]);
        }
        else {
            printf("%c is not alphabetic\n", str[i]);
        }
        i++;
    }
    return 0;
}
C is alphabetic
+ is not alphabetic
+ is not alphabetic
  is not alphabetic
J is alphabetic
a is alphabetic
v is alphabetic
a is alphabetic
  is not alphabetic
C is alphabetic
# is not alphabetic

isupper() 一个字符?大写

头文件:

头文件:ctype.h

语法原型:

int isupper(int c);

参数说明:
参数 c 表示要检测的字符。

【功能】
strcat() 函数把 strSource 所指向的字符串追加到 strDestination 所指向的字符串的结尾

【返回值】

  • 返回值为非 0(真)表示 c 是大写字母
  • 返回值为 0(假)表示 c 不是大写字母。

【实例】使用C语言 isupper() 函数判断字符串中的字符是否是大写字母,如果是,那么转换为小写字母。

#include <stdio.h>
#include <ctype.h>
int main ()
{
    int i = 0;
    char str[] = "C++ Java Python C# Linux Golang Shell\n";
    char c;
    while(str[i])
    {
        c = str[i];
        if(isupper(c)) c = tolower(c);
        putchar(c);
        i++;
    }
    return 0;
}
c++ java python c# linux golang shell

islower() 一个字符?小写

头文件:

头文件:ctype.h

语法原型:

int islower(int c);

参数说明:
参数 c 表示要检测的字符或者 ASCII 码。

【返回值】

  • 返回值为非 0(真)表示 c 是小写字母
  • 返回值为 0(假)表示 c 不是小写字母。

【实例】使用C语言 islower() 函数判断一个字符串中的字符是否是小写字母,如果是,那么转换为大写字母。

#include <stdio.h>
#include <ctype.h>
int main ()
{
    int i=0;
    char str[] = "c++ java python c#\n";
    char c;
    while(str[i])
    {
        c = str[i];
        if (islower(c)) c = toupper(c);
        putchar(c);
        i++;
    }
    return 0;
}
C++ JAVA PYTHON C#

isdigit()函数:一个字符?数字

头文件:

头文件:ctype.h

语法原型:

int isdigit(int c);

参数说明:
参数 c 表示要检测字符或者 ASCII 码。

【返回值】

  • 返回值为非 0(真)表示 c 是数字
  • 返回值为 0(假)表示 c 不是数字。

【实例】使用C语言 isdigit() 函数判断用户输入的字符串中有多少个数字。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
    char str[50] = { 0 };
    int i, len, n=0;
    gets(str);
    for (i = 0, len = strlen(str); i < len; i++) {
        if (isdigit(str[i])) {
            n++;
        }
    }
    printf("There are %d numbers in str\n", n);
    return 0;
}
we232sdji&^*2309d
There are 7 numbers in str

isalnum()函数:一个字符? 字母或者数字

头文件:

头文件:ctype.h

语法原型:

int isalnum(int c);

参数说明:
参数 c 表示要检测字符或者 ASCII 码。

【返回值】

  • 返回值为非 0(真)表示 c 是字母或者数字
  • 返回值为 0(假)表示 c 不是字母或者数字。

【实例】使用C语言 isalnum() 函数统计一个字符串中有多少个字母或数字。

#include <stdio.h>
#include <ctype.h>
int main()
{
    int i = 0, n = 0;
    char str[] = "*http://c.biancheng.net is 7 years old";
    while (str[i])
    {
        if (isalnum(str[i])) n++;
        i++;
    }
    printf("There are %d characters in str is alphanumeric.\n", n);
    return 0;
}
There are 28 characters in str is alphanumeric.

isspace() 一个字符 ? 特殊字符(空白符)

字符ASCII码 (十六进制)说明 (缩写)
’ ’0x20空格 (SPC)
‘\t’0x09水平制表符 (TAB)
‘\n’0x0a换行符 (LF)
‘\v’0x0b垂直制表符 (VT)
‘\f’0x0c换页 (FF)
‘\r’0x0d回车 (CR)

头文件:

头文件:ctype.h

语法原型:

int isspace(int c);

参数说明:
参数 c 表示要检测的字符。

【返回值】

  • 返回值为非 0(真)表示c是空白符
  • 返回值为 0(假)表示c不是空白符。

【实例】使用C语言 isspace() 函数检测字符串中是否存在空白字符,如果存在,请替换为换行符。

#include <stdio.h>
#include <ctype.h>
int main()
{
    char c;
    int i = 0;
    char str[] = "Linux C++\tPython Shell\nC# Java\n";
    while (str[i])
    {
        c = str[i];
        if (isspace(c)) c = '\n';
        putchar(c);
        i++;
    }
    return 0;
}
Linux
C++
Python
Shell
C#
Java

tolower() 把字符转化为小写

int tolower(int c);

【参数】
c – 这是要被转换为小写的字母。
【返回值】

  • 如果 c 有相对应的小写字母,则该函数返回 c 的小写字母
  • 否则 c 保持不变。

【实例】

#include <stdio.h>
#include <ctype.h>
 
int main()
{
   int i = 0;
   char c;
   char str[] = "RUNOOB";
 
   while( str[i] ) 
   {
      putchar(tolower(str[i]));
      i++;
   }
 
   return(0);
}
runoob

toupper() 把字符转化为大写

int toupper(int c);

【参数】
c – 这是要被转换为大写的字母。
【返回值】

  • 如果 c 有相对应的大写字母,则该函数返回 c 的大写字母
  • 否则 c 保持不变。

【实例】

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

int main()
{
   int i = 0;
   char c;
   char str[] = "runoob";
   
   while(str[i])
   {
      putchar (toupper(str[i]));
      i++;
   }
   
  return(0);
}
RUNOOB

数学 库函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值