AI大模型教程
一起来学习

12.【C语言学习笔记】字符函数和字符串函数(strlen获取字符串长度、strcpy拷贝、strcat拼接、strcmp比较、strstr查找、strtok分割、strerror、perror)

目录

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

1.1 字符分类函数

1.2 字符转换函数

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

2.1 字符串函数汇总

2.2 strlen的使用和模拟实现(获取长度)

2.3 strcpy的使用及模拟实现(字符串拷贝)

2.4 strcat的使用和模拟实现(字符串拼接)

2.5 strcmp的使用和模拟实现(字符串比较)

2.6 strncpy函数的使用(指定个数拷贝)

2.7 strncat函数的使用(指定个数拼接)

2.8 strncmp函数的使用(指定个数比较)

2.9 strstr的使用和模拟实现(字符串查找)

2.10 strtok函数的使用(指定字符分割)

2.11 strerror函数的使用(获取错误信息)

2.12 perror函数的使用(封装了strerror,打印错误信息)


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

1.1 字符分类函数

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

函数 如果参数符合下列条件就返回真
iscntrl 任何控制字符
isspace 空白字符:空格 ‘ ’,换页 ‘f’ ,换行 ‘n’,回车 ‘r’,制表符 ‘t’ 或者垂直制表符 ‘v’
isdigit 十进制数字0~9
isxdigit 十六进制数字,包括所有十进制数字,a~f,大写字母A~F
islower 小写字母a~z
isupper 大写字符A~Z
isalpha 字母a~z 或 A~Z
isalnum 字母或数字,a~z,A~Z,0~9
ispunct 标点符号,任何不属于数字或者字母的题型字符(可打印)
isgraph 任何图形字符
isprint 任何可打印字符,包括图形字符和空白字符

        这类函数的使用方法类似,只讲一个函数

int islower ( int c );

        islower 是能够判断参数部分的c 是否是小写字母的。通过返回值来说明是否是小写字母,如果小写字母就返回非0的整数,如果不是小写字母,则返回0

// 判断字母是否是小写,如果是,则变成大写
#include 
#include 
int main()
{
	int i = 0;
	char str[] = "Test String.n";
	char c;
	while (str[i])
	{
		c = str[i];
		if (islower(c))
			c -= 32;
		putchar(c);
		i++;
	}
	return 0;
}

1.2 字符转换函数

函数 功能
tolower 将传进去的大写字母转小写
toupper 将传进去的小写字母变大写
int tolower ( int c ); //将参数传进去的大写字母转小写
int toupper ( int c ); //将参数传进去的小写字母转大写

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

#include 
#include 
int main()
{
	int i = 0;
	char str[] = "Test String.n";
	char c;
	while (str[i])
	{
		c = str[i];
		if (islower(c))
			c = toupper(c);
		putchar(c);
		i++;
	}
	return 0;
}

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

2.1 字符串函数汇总

        字符串函数的使用要包含头文件

size_t strlen(const char* str);	//获取之前字符个数
char* strcpy(char* destination, const char* source);	//字符串拷贝
char* strcat(char* destination, const char* source);	//字符串拼接
int strcmp(const char* str1, const char* str2);		// 字符串比较
char* strncpy(char* destination, const char* source, size_t num);	//指定个数拷贝
char* strncat(char* destination, const char* source, size_t num);	//指定个数拼接
int strncmp(const char* str1, const char* str2, size_t num);		//指定个数比较
char* strstr(const char* str1, const char* str2);	//字符串查找
char* strtok(char* str, const char* sep);	//指定字符分割
char* strerror (int errnum);	//错误信息获取
函数链接 功能
strlen 获取前字符个数
strcpy 字符串拷贝
strcat 字符串拼接
strcmp 字符串比较
strncpy 指定个数拷贝
strncat 指定个数拼接
strncmp 指定格式比较
strstr 字符串查找
strtok 指定字符分割
strerror 错误信息获取

2.2 strlen的使用和模拟实现(获取长度)

2.2.1 strlen函数的使用

size_t strlen(const char* str);

        1. 求的是字符串的长度,统计的是字符串中之前的字符的个数(不包含‘’)

        2. 参数指向的字符串必须要以” 结束。

        3. 注意函数的返回值为size_t,是无符号的( 易错)(代码解释)

        4. strlen的使用需要包含头文件 string.h

#include 
#include 
int main()
{
	const char* str1 = "abcdef";
	const char* str2 = "bbb";
	//		size_t   -   size_t   =   size_t
	// 两个size_t类型相减还是size_t类型,3-6=-3,size_t应该是个很大的正数,所以str2>str1
	if (strlen(str2) - strlen(str1) > 0)
	{
		printf("str2>str1n");
	}
	else
	{
		printf("srt1>str2n");
	}
	return 0;
}

2.2.2 strlen函数的模拟实现(1.计数器的方式;2.指针-指针;3.递归)

//计数器方式
int my_strlen(const char* str)
{
	int count = 0;
	assert(str);
	while (*str)
	{
		count++;
		str++;
	}
	return count;
}
//指针-指针的方式
int my_strlen(char* s)
{
	assert(str);
	char* p = s;
	while (*p != ‘’)
		p++;
	return p - s;
}
//不能创建临时变量计数器
int my_strlen(const char* str)
{
	assert(str);
	if (*str == '')
		return 0;
	else
		return 1 + my_strlen(str + 1);
}

2.3 strcpy的使用及模拟实现(字符串拷贝)

2.3.1 strcpy函数的使用

char* strcpy(char* destination, const char* source);

        • Copies the C string pointed by source into the array pointed by destination, including theterminating null character (and stopping at that point).

        • 源字符串必须以” 结束。

        • 会将源字符串中的” 拷贝到目标空间。

        • 目标空间必须足够大,以确保能存放源字符串。

        • 目标空间必须可修改。

        • 学会模拟实现。

#include

int main()
{
	char arr1[] = "hello world";
	char arr2[] = "xxxxxxxxxxxxxx";
	//char* arr2 = "xxxxxxxxxxxxxxx";	// 常量字符串不能修改

	strcpy(arr2, arr1);
	printf("%sn", arr2);
	return 0;
}

2.3.2 strcpy函数的模拟实现

#include
#include
char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest);
	assert(src);
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[] = "hello world";
	char arr2[] = "xxxxxxxxxxxxxx";

	my_strcpy(arr2, arr1);
	printf("%sn", arr2);
	return 0;
}

2.4 strcat的使用和模拟实现(字符串拼接)

2.4.1 strcat函数的使用

char* strcat(char* destination, const char* source);	//字符串拼接

        • Appends a copy of the source string to the destination string. The terminating null characterin destination is overwritten by the first character of source, and a null-character is includedat the end of the new string formed by the concatenation of both in destination.

        • 源字符串必须以” 结束。

        • 目标字符串中也得有 ,否则没办法知道追加从哪里开始。

        • 目标空间必须有足够的大,能容纳下源字符串的内容。

        • 目标空间必须可修改。

        • 字符串自己给自己追加,如何?(答:会死循环,最后导致程序崩溃)

#include
#include

int main()
{
	char arr1[20] = "hello";
	char arr2[] = " world";

	strcat(arr1, arr2);
	printf("%sn", arr1);
	return 0;
}

2.4.2 strcat函数的模拟实现

#include
#include
#include
char* my_strcat(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest!=NULL);
	assert(src!=NULL);
	while (*dest != '')
		dest++;    // 不能直接在判断条件里++,会跳过一个导致打印不出拼接的字符串
	while (*dest++ = *src++)
		;
	return ret;
}
int main()
{
	char arr1[20] = "hello";
	char arr2[] = "world";

	my_strcat(arr1, arr2);
	printf("%sn", arr1);
	return 0;
}

2.5 strcmp的使用和模拟实现(字符串比较)

2.5.1 strcmp函数的使用

int strcmp(const char* str1, const char* str2);		// 字符串比较

        • This function starts comparing the first character of each string. If they are equal to eachother, it continues with the following pairs until the characters differ or until a terminatingnull-character is reached.

        • 标准规定:

        ◦ 第一个字符串大于第二个字符串,则返回大于0的数字

        ◦ 第一个字符串等于第二个字符串,则返回0

        ◦ 第一个字符串小于第二个字符串,则返回小于0的数字

        ◦ 那么如何判断两个字符串? 比较两个字符串中对应位置上字符ASCII码值的大小。

#include
#include
int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abc";

	//if(arr1 == arr2)	// 比较的是地址
	//if("abcdef" == "abc")	// 比较的也是地址
	// 如果需要比较两个字符串,需要使用strcmp
	int ret = strcmp(arr1, arr2);
	printf("%dn", ret);
	return 0;
}

2.5.2 strcmp函数的模拟实现

#include
#include
#include

int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == '')
			return 0;
		str1++;
		str2++;
	}
	return *str1 - *str2;
}

int main()
{
	char arr1[] = "abq";
	char arr2[] = "abcdef";

	//if(arr1 == arr2)	// 比较的是地址
	//if("abcdef" == "abc")	// 比较的也是地址
	// 如果需要比较两个字符串,需要使用strcmp
	int ret = my_strcmp(arr1, arr2);
	printf("%dn", ret);
	return 0;
}

2.6 strncpy函数的使用(指定个数拷贝)

char* strncpy(char* destination, const char* source, size_t num);

        • 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.

        • 拷贝num个字符从源字符串到目标空间。

        • 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。

2.7 strncat函数的使用(指定个数拼接)

char* strncat(char* destination, const char* source, size_t num);

        • Appends the first num characters of source to destination, plus a terminating null-character.(将source指向字符串的前num个字符追加到destination指向的字符串末尾,再追加一个 字符)。

        • If the length of the C string in source is less than num, only the content up to the terminatingnull-character is copied.(如果source 指向的字符串的长度小于num的时候,只会将字符串中到 的内容追加到destination指向的字符串末尾)。

/* strncat example */
#include 
#include 
int main()
{
	char str1[20];
	char str2[20];
	strcpy(str1, "To be ");
	strcpy(str2, "or not to be");
	strncat(str1, str2, 6);
	printf("%sn", str1);
	return 0;
}

2.8 strncmp函数的使用(指定个数比较)

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

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

2.9 strstr的使用和模拟实现(字符串查找)

2.9.1 strstr函数的使用

char* strstr(const char* str1, const char* str2);

        Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.(函数返回字符串str2在字符串str1中第一次出现的位置)。

        The matching process does not include the terminating null-characters, but it stops there.(字符串的比较匹配不包含 字符,以 作为结束标志)。

#include
#include

int main()
{
	char arr1[] = "abcdefabcdef";
	char arr2[] = "cdef";
	char* ret = strstr(arr1, arr2);
	if (ret == NULL)
		printf("找不到n");
	else
		printf("%sn", ret);
	return 0;
}

2.9.2 strstr函数的模拟实现

#include
#include

char* my_strstr(const char* str1, const char* str2)
{
	const char* s1 = NULL;
	const char* s2 = NULL;
	const char* cur = str1;
	if (*str2 == '')
		return (char*)str1;
	while (*cur)
	{
		s1 = cur;
		s2 = str2;
		while (*s1 !='' && *s2 !=''&& *s1 == *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '')
		{
			return cur;
		}
		cur++;
	}
	return NULL;
}

int main()
{
	char arr1[] = "abcdeabcdef";
	char arr2[] = "cdef";
	char* ret = my_strstr(arr1, arr2);
	if (ret == NULL)
		printf("找不到n");
	else
		printf("%sn", ret);
	return 0;
}

2.10 strtok函数的使用(指定字符分割)

char* strtok(char* str, const char* sep);

        • sep参数指向一个字符串,定义了用作分隔符的字符集合

        • 第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记

        • strtok函数找到str中的下一个标记,并将其用 结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)

        • strtok函数的第一个参数不为NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。

        • strtok函数的第一个参数为NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记。

        • 如果字符串中不存在更多的标记,则返回NULL 指针。

#include
#include

int main()
{
	char str[] = "zpengwei@yeah.net";
	char str2[30] = { 0 };
	strcpy(str2, str);
	const char* sep = "@.";
	char* ret = NULL;
	for (ret = strtok(str2, sep); ret != NULL; ret = strtok(NULL, sep))
	{
		printf("%sn", ret);
	}

	//ret = strtok(str2, sep);
	//printf("%sn", ret);
	//ret = strtok(NULL, sep);
	//printf("%sn", ret);
	//ret = strtok(NULL, sep);
	//printf("%sn", ret);

	return 0;
}

2.11 strerror函数的使用(获取错误信息)

char* strerror (int errnum);

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

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

// strerror函数的使用
#include 
#include 
#include 
//我们打印一下0~10这些错误码对应的信息
int main()
{
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		printf("%sn", strerror(errno));	// 打印错误信息
		return 1;
	}
	fclose(pf);
	return 0;
} 

2.12 perror函数的使用(封装了strerror,打印错误信息)

        perror函数,perror函数相当于一次将上述代码中的printf那一行行完成了,直接将错误信息打印出来。

        perror函数打印完参数部分的字符串后,再打印一个冒号和一个空格,再打印错误信息。

#include 
//我们打印一下0~10这些错误码对应的信息
int main()
{
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("zhangsan");	// 打印错误信息
		return 1;
	}
	fclose(pf);
	return 0;
} 

文章来源于互联网:12.【C语言学习笔记】字符函数和字符串函数(strlen获取字符串长度、strcpy拷贝、strcat拼接、strcmp比较、strstr查找、strtok分割、strerror、perror)

相关推荐: 我的天!这个 Stable Diffusion 新模型换脸效果真不错!

ControlNet 是 Stable Diffusion Web UI 中功能最强大的插件。基于 ControlNet 的各种控制类型让 StableDiffusion 成为 AI绘图工具中最可控的一种。 IP Adapter 就是其中的一种非常有用的控制类…

赞(0)
未经允许不得转载:5bei.cn大模型教程网 » 12.【C语言学习笔记】字符函数和字符串函数(strlen获取字符串长度、strcpy拷贝、strcat拼接、strcmp比较、strstr查找、strtok分割、strerror、perror)
分享到: 更多 (0)

AI大模型,我们的未来

小欢软考联系我们