• C语言.字符函数与字符串函数


    1.字符分类函数

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

    在这里插入图片描述

    这些函数的使用方法非常类似,就那一个来举例,其他非常的类似:

    int islower(int c);
    
    • 1
    • islower 是能够判断参数部分的 c 是否是小写字母的。

    • 通过返回值来说明是否是小写字母,如果是小写字母就返回非0的整数,如果不是小写字母,则返回
      0。

    练习:

    写一个代码,将字符串中的小写字母转大写,其他字符不变。

    //方法1
    #include 
    #include 
    
    int main()
    {
    	int i = 0;
    	char str[] = "Hello World\n";
    	char c;
    	while (str[i])
    	{
    		c = str[i];
    		if (islower(c))
    			c = c - 32;
    
    			putchar(c);//HELLO WORLD
    			i++;
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2.字符转换函数

    C语言提供了2个字符转换函数:

    int tolower(int c)//将参数传进出的大写字母转小写
    int toupper(int c)//将参数传进出的小写字母转大写
    
    • 1
    • 2

    上面的代码,我们将小写转大写,是 -32 完成的效果,有了转换函数,就可以直接使用 tolower 函数。

    //方法2:
    #include 
    #include 
    
    int main()
    {
    	int i = 0;
    	char str[] = "Hello World\n";
    	char c;
    	while (str[i])
    	{
    		c = str[i];
    		if (islower(c))
    			c = toupper(c);
    
    		putchar(c);//HELLO WORLD
    		i++;
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3.strlen 的使用和模拟实现

    size_t strlen(const char* str);
    
    • 1
    1. 字符串以’\0’作为结束标志,strlen函数返回是在字符串中’\0’前面的字符个数
    2. 参数指向的字符串必须要以’\0’结束
    3. 函数的返回值为size_t,是无符号的
    4. strlen的使用需要包含头文件(string.h)
    #include 
    #include 
    
    int main()
    {
    	const char* str1 = "abcdef";
    	const char* str2 = "bbb";
    
    	if (strlen(str2) - strlen(str1))
    	{
    		printf("str2>str1\n");//str2>str1
    	}
    	else
    	{
    		printf("str2);
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    strlen的模拟实现:
    方法1:

    #include 
    #include 
    #include 
    
    //计算器的实现
    int my_strlen(const char* str)
    {
    	int count = 0;
    	assert(str);
    	while (*str)
    	{
    		count++;
    		str++;
    	}
    	return count;
    }
    
    int main()
    {
    	char str[] = "abcdef";
    	int ret = my_strlen(str);
    
    	printf("%d\n", ret);//6
    
    	return 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

    方法2:

    #include 
    #include 
    #include 
    
    //不能创建临时变量计数器,利用递归的方式来解决
    int my_strlen(const char* str)
    {
    	assert(str);
    	if (*str == '\0')
    		return 0;
    	else
    		return 1 + my_strlen(str + 1);
    }
    
    int main()
    {
    	char str[] = "abcdef";
    	int ret = my_strlen(str);
    
    	printf("%d\n", ret);//6
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    方法3:

    #include 
    #include 
    #include 
    
    //指针-指针的方式
    int my_strlen(const char* str)
    {
    	assert(str);
    	char* p = str;
    	while (*p != '\0')
    	{
    		p++;
    	}
    	return p - str;
    }
    
    int main()
    {
    	char str[] = "abcdef";
    	int ret = my_strlen(str);
    
    	printf("%d\n", ret);//6
    
    	return 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

    4.strcpy 的使用和模拟实现

     char* strcpy(char * destination, const char * source );
    
    • 1
    1. Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
    2. 源字符必须以’\0’结束
    3. 会将源字符的’\0’拷贝到目标空间中
    4. 目标空间必须可修改。

    strcpy的模拟实现:

    #include 
    #include 
    //1.参数的顺序
    //2.函数的功能
    //3.assert
    //4.const修饰指针
    //5.函数返回值
    char* my_strcpy(char* dest, const char* src)
    {
    	char* ret = dest;
    	assert(dest && src);
    	while (*dest++ = *src++)
    	{
    		;
    	}
    	return ret;
    }
    int main()
    {
    	char str1[] = "abcdef";
    	char str2[20] = { 0 };
    	//拷贝:
    	my_strcpy(str2, str1);
    
    	printf("%s\n", str2);//abcdef
    	//printf(str2);
    
    	return 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

    5.strcat 的使用和模拟实现

    1. Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
    2. 源字符串必须以’\0’结束
    3. 目标字符串中必须也得有\0,否则没办法知道追加从哪开始
    4. 目标空间必须有足够的空间,能够容纳下源字符串的内容
    5. 目标空间必须可修改

    模拟实现strcat函数:

    #include 
    #include 
    
    char* my_strcat(char* dest, const char* src)
    {
    	char* ret = dest;
    	assert(dest && src);
    	//让dest指针走到'\0'位置
    	while (*dest)
    	{
    		dest++;
    	}
    	while (*dest++ = *src++)
    	{
    		;
    	}
    	return ret;
    }
    int main()
    {
    	char str1[] = "abc";
    	char str2[10] = "cdef";
    	//追加
    	my_strcat(str2, str1);
    	printf(str2);//cdefabc
    
    	return 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

    6.strcmp 的使用和模拟实现

    1. 第一个字符串大于二个字符串,则返回大于0的数字
    2. 第一个字符串等于第二个字符串,则返回0
    3. 第一个字符串小于第二个字符串,则返回小于0的数字
    4. 那么如何判断两个字符串? 比较两个字符串中对应位置上字符ASCII码值的大小

    strcmp函数的模拟实现:

    #include 
    #include 
    
    int my_strcmp(const char* str2, const char* str1)
    {
    	int ret = 0;
    	assert(str1 && str2);
    	//比较
    	while (*str2 == *str1)
    	{
    		if (*str1 == '\0')
    			return 0;
    		str1++;
    		str2++;
    	}
    	return *str2 - *str1;
    }
    
    int main()
    {
    	char str1[] = "abcdef";
    	char str2[] = "ace";
    
    	int ret = my_strcmp(str2, str1);
    	printf("%d\n", ret);//1
    
    	return 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

    7.strncpy 函数的使用

    char * strncpy ( char * destination, const char * source, size_t num );
    
    • 1
    1. Copies the first num characters of source to destination. If the end of the source C string(which is signaled by a null-character) is found before num characters have been copied,destination is padded with zeros until a total of num characters have been written to it.
    2. 作用:拷贝num个字符串到目标空间中。
    3. 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标空间的后面追加0,知道num个。

    8.strncat 函数的使用

    char* strncat(char* destination, const char* source, size_t num);
    
    • 1
    1. Appends the first num characters of source to destination, plus a terminating null-character.(将source指向字符串的前num个字符追加到destination指向的字符串末尾,再追加一个 \0 字符)。
    2. If the length of the C string in source is less than num, only the content up to the terminatin null-character is copied.(如果source 指向的字符串的长度小于num的时候,只会将字符串中到 \0 的内容追加到destination指向的字符串末尾)。
    #include 
    #include 
    
    int main()
    {
    	char str1[20];
    	char str2[20];
    	strcpy(str1, "To be ");
    	strcpy(str2, "or not to be");
    	strncat(str1, str2, 15);
    	printf("%s\n", str1);
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    9.strncmp 函数的使用

    int strncmp(const char* str1, const char* str2, size_t num);
    
    • 1

    比较str1和str2的前num个字符,如果相等就继续往后比较,最多比较num个字母,如果提前发现不一
    样,就提前结束,大的字符所在的字符串大于另外一个。如果num个字符都相等,就是相等返回0.

    在这里插入图片描述

    10. strstr 的使用和模拟实现

    1. Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.(函数返回字符串str2在字符串str1中第一次出现的位置)。
    2. The matching process does not include the terminating null-characters, but it stops there.(字符串的比较匹配不包含 \0 字符,以 \0 作为结束标志)。
    #include 
    #include 
    
    int main()
    {
    	char str[] = "This is a simple string";
    	char* pch;
    	pch = strstr(str, "simple");
    	strncpy(pch, "sample", 6);
    	printf("%s\n", str);//This is a sample string
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    strstr的模拟实现

    #include 
    #include 
    
    char* my_strstr(const char* str1, const char* str2)
    {
    	const char* s1 = NULL;
    	const char* s2 = NULL;
    	const char* cur = str1;
    
    	while (*cur)
    	{
    		s1 = cur;
    		s2 = str2;
    		while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
    		{
    			s1++;
    			s2++;
    		}
    		//找到了
    		if (*s2 == '\0')
    			return cur;
    		cur++;
    	}
    	//假如cur都走完一遍了,还是没有,就返回NULL
    	return NULL;
    }
    
    int main()
    {
    	char str1[] = "hello world";
    	char str2[] = "llo";
    	char* ret = my_strstr(str1, str2);
    
    	if (ret == NULL)
    		printf("找不到\n");
    	else
    		printf("%s\n", ret);
    
    	return 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    画图分析:
    在这里插入图片描述

    11.strtok 函数的使用

    char * strtok ( char * str, const char * sep);
    
    • 1
    1. sep参数指向一个字符串,定义了用作分隔符的字符集合
    2. 第一个参数指定一个字符串,它包含了0个或多个由sep字符串中的一个或者多个分隔符分割的标记。
    3. strtok函数找到str中的下一个标记,并将其用\0结尾,返回一个指向这个标记的指针。(strtok函数会修改被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
    4. strtok函数的第一个参数不为 NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
    5. strtok函数的第一个参数为NULL,函数将在同一个字符串被保存的位置开始,查找下一个标记。
    6. 如果字符串中不存在更多的标记,则返回NULL指针。
    #include 
    #include 
    
    int main()
    {
    	char arr[] = "198.650.955.091";
    	char* sep = ".";
    	char* str = NULL;
    
    	for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
    	{
    		printf("%s\n", str);
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    12.strerror函数的使用

    char* strerror(int errnum);
    
    • 1

    strerror 函数可以把参数部分错误码对应的错误信息的字符串地址返回来。

    在不同的系统和C语言标准库的实现中都规定了一些错误码,一般是放在 errno.h 这个头文件中说明的,C语言程序启动的时候就会使用一个全局的变量errno来记录程序的当前错误码,只不过程序启动的时候errno是0,表示没有错误,当我们在使用标准库中的函数的时候发生了某种错误,就会将对应的错误码,存放在errno中,而一个错误码的数字是整数很难理解是什么意思,所以每一个错误码都是有对应的错误信息的。strerror函数就可以将错误对应的错误信息字符串的地址返回

    #include 
    #include 
    #include 
    
    int main()
    {
    	int i = 0;
    	for (i = 0; i <= 10; i++)
    	{
    		printf("%s\n", strerror(i));
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在Windows11+VS2022环境下输出的结果如下:

    No error
    Operation not permitted
    No such file or directory
    No such process
    Interrupted function call
    Input / output error
    No such device or address
    Arg list too long
    Exec format error
    Bad file descriptor
    No child processes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    举例:

    #include 
    #include 
    #include 
    
    int main()
    {
    
    	FILE* pFile;
    	pFile = fopen("unexist.ent", "r");
    	if (pFile == NULL)
    		printf("Error opening file unexist.ent: %s\n", strerror(errno));
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    输出:

    Error opening file unexist.ent: No such file or directory
    
    • 1

    也可以了解一下 perror 函数,perror函数相当于一次将上述代码中的第11行完成了,直接将错误信息打印出来。perror函数打印完参数部分的字符串后,再打印一个冒号和一个空格,再打印错误信息。

    #include 
    #include 
    #include 
    
    int main()
    {
    
    	FILE* pFile;
    	pFile = fopen("unexist.ent", "r");
    	if (pFile == NULL)
    		perror("Error opening file unexist.ent");
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    输出:

    Error opening file unexist.ent: No such file or directory
    
    • 1
  • 相关阅读:
    「免费申请」基于Arm Cortex-M4的兆易创新GD32F427高性能开发板试用活动
    看完100%会写毕业论文
    PHP的file_get_contents函数读取远程图片慢,耗时长的解决办法
    JVM类文件结构
    「项目管理工具」进度猫如何实现可视化?
    xxl-job任务调度2.0.2升级到2.3.0版本,执行器改造过程中经验总结
    【机器学习】带你轻松理解什么是强化学习中的贝尔曼方程
    国内外数据保护(灾备、备份、恢复、复制、归档)厂商
    C++如何在main函数开始之前(或结束之后)执行一段逻辑?
    odps函数
  • 原文地址:https://blog.csdn.net/Joseit/article/details/138057164