zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C语言---字符串函数总结

C语言 函数 总结 字符串 ---
2023-09-14 09:12:40 时间

在这里插入图片描述

🚀write in front🚀
📝个人主页:认真写博客的夏目浅石.
🎁欢迎各位→点赞👍 + 收藏⭐️ + 留言📝
📣系列专栏:夏目的C语言宝藏
💬总结:希望你看完之后,能对你有所帮助,不足请指正!共同学习交流 🖊
✉️如果无聊的话,就来逛逛我的博客栈吧stack-frame.cn


前言

函数的学习离不开这些


提示:以下是本篇文章正文内容,下面案例可供参考

函数介绍以及模拟实现

1.1 strlen函数

size_t strlen ( const char * str );
  • 字符串已经'\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
  • 参数指向的字符串必须要以 '\0' 结束。
  • 注意函数的返回值为size_t,是无符号的

设置为size_t的原因介绍:

strlen求字符串长度的,求出的长度是不可能为负数的
所以返回类型设置为size_t也是合情合理的
typedef unsigned int size_t

验证代码:

#include<stdio.h>
#include<string.h>

int main()
{
	//数学上:3-6=-3;
	//实际上:size_t所以俩差值是一个很大的数 
	if(strlen("abc") - strlen("abcdef") > 0)
	{
		printf(">\n");
	}
	else
	{
		printf("<=\n");
	}
	
	return 0;
}

运行图示:
在这里插入图片描述

strlen函数的模拟实现

方法一:遍历法

#include<stdio.h>
#include<assert.h>
int my_strlen(char* str)
{
	assert(str != NULL);
	int cnt=0;
	while(*str!='\0')
	{
		cnt++;
		str++;
	}
	return cnt;
}
int main()
{
	char arr[]="abcdef";
	int len=my_strlen(arr);
	printf("%d\n",len);
	return 0;
}

方法二:递归法

#include<stdio.h>
#include<assert.h>
int my_strlen(const char* str)
{
	assert(str!=NULL);
	if(*str!='\0')
		return 1+my_strlen(str+1);
	else return 0;
}
int main()
{
	char arr[]="abcdef";
	int len=my_strlen(arr);
	printf("%d\n",len);
	return 0;
}

方法三:减指法

#include<stdio.h>
#include<assert.h>
int my_strlen(const char* str)
{
	const char* start=str;
	assert(str!=NULL);
	while(*str)
	{
		str++;
	}
	return str-start;
}

int main()
{
	char arr[]="abcdef";
	int len=my_strlen(arr);
	printf("%d\n",len);
	return 0;
}

1.2 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).
  • 源字符串必须以 '\0' 结束。
  • 会将源字符串中的 '\0' 拷贝到目标空间。
  • 目标空间必须足够大,以确保能存放源字符串。
  • 目标空间必须可变。

strcpy函数的模拟实现

方法一:遍历法

#include<stdio.h>
#include<assert.h>
char* my_strcpy(char* dest , const char* src)
{
	char *ret=dest;
	assert(dest&&src);
	while(*dest++ = *src++)
	{
		;
	}
	return ret;
}

int main()
{
	char arr[]="xxxxxxxxxx";
	char a[]="abcdef";
	my_strcpy(arr,a);
	
	printf("%s",arr);
	return 0;
}

方法二:遍历法

#include<stdio.h>
#include<assert.h>
void my_strcpy(const char*str,char* ptr)
{
	assert(str!=NULL);
	assert(ptr!=NULL);
	while(*str!='\0')
	{
		*ptr=*str;
		str++;
		ptr++;
	}
}
int main()
{
	char arr[]="xxxxxxxxxx";
	char a[]="abcdef";
	my_strcpy(arr,a);
	
	printf("%s",arr);
	return 0;
}

1.3 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.
  • 源字符串必须以 '\0' 结束。
  • 目标空间必须有足够的大,能容纳下源字符串的内容。
  • 目标空间必须可修改。

函数的使用:

#include<stdio.h>
#include<assert.h>
#include<string.h>

int main()
{
	char arr[20]="hello ";
	strcat(arr,"world");
	printf("%s",arr); 
	
	return 0;
}

在这里插入图片描述

strcat函数的模拟实现

#include<stdio.h>
#include<assert.h>
#include<string.h>

char* my_strcat(char* dest,const char* src)
{
	assert(dest && src);
	char* start= dest;
	//1.寻找'\0'
	while(*dest)
	{
		dest++;
	}
	//2.追加
	while(*dest++ = *src++)
	{
		;
	}
	return start; 
}

int main()
{
	char arr[20]="hello ";
	my_strcat(arr,"world");
	printf("%s",arr); 
	return 0;
}

1.4 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的数字

strcmp函数的模拟实现

#include<stdio.h>
#include<string.h>
#include<assert.h>

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

int main()
{
	char arr1[]="ab";
	char arr2[]="abcde";
	int ret=my_strcmp(arr1,arr2);
	printf("%d\n",ret);
	
	return 0;
}

1.5 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个。

函数的使用:

#include<stdio.h>
#include<string.h>
#include<assert.h>

int main()
{
	char arr1[20]="xxxxxxxxxx";
	strncpy(arr1,"abcdef",3);
	printf("%s\n",arr1);
	
	return 0;
}

在这里插入图片描述

1.6 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.
  • If the length of the C string in source is less than num, only the content up to the terminatingnull-character is copied

函数的使用:

#include <stdio.h>
#include <string.h>
int main ()
{
 	char str1[20];
 	char str2[20];
 	strcpy (str1,"To be ");
 	strcpy (str2,"or not to be");
 	strncat (str1, str2, 6);
 	puts (str1);
 	return 0;
}

1.7 strncmp函数

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

函数的使用:

#include <stdio.h>
#include <string.h>
int main ()
{
  	char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
  	int n;
  	puts ("Looking for R2 astromech droids...");
  	for (n=0 ; n<3 ; n++)
  	if (strncmp (str[n],"R2xx",2) == 0)
 	{
   	 	printf ("found %s\n",str[n]);
 	}
  return 0;
}

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

函数的使用:

#include<stdio.h>
#include<string.h>
#include<assert.h>

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

模拟实现:

#include<stdio.h>
#include<string.h>
#include<assert.h>

char* my_strstr(char* str1,char* str2)
{
	assert(str1&&str2);
	if(*str2=='\0')
	{
		return str1;
	}
	char* s1=str1;
	char* s2=str2;
	char* cp=str1;
	while(*cp)
	{
		s1=cp;
		s2=str2;
		while(*s1!='\0' && *s2!='\0' && *s1==*s2)
		{
			++s1;
			++s2;
		}
		if(*s2=='\0')
		{
			return cp;
		}
		cp++;
	}
	return NULL;
}

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

1.9 strtok函数

char * strtok ( char * str, const char * sep );
  • sep参数是个字符串,定义了用作分隔符的字符集合
  • 第一个参数指定一个字符串,它包含了0个或者多个sep字符串中一个或者多个分隔符分割的标记。
  • strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:
    strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容
    并且可修改。)
  • strtok函数的第一个参数不为 NULL ,函数将找到str第一个标记strtok函数将保存它在字符串中的位置。
  • strtok函数的第一个参数NULL函数将在同一个字符串中被保存的位置开始,查找下一个标
    记。
  • 如果字符串中不存在更多的标记,则返回 NULL 指针。
#include<stdio.h>
#include<string.h>

int main()
{
	char arr[]="192#168.120.85";
	char* p="#.";
	char buf[20]={0};
	strcpy(buf,arr);
	char* ret=NULL;
	for(ret=strtok(buf,p);ret!=NULL;ret=strtok(NULL,p))
	{
		printf("%s\n",ret);
	}
	
	return 0;
}

1.10 strerror函数

char * strerror ( int errnum );
  • 返回错误码,所对应的错误信息。

在这里插入图片描述

1.11 memcpy函数

void * memcpy ( void * destination, const void * source, size_t num );
  • 函数memcpysource的位置开始向后复制num个字节的数据到destination的内存位置。
  • 这个函数在遇到 '\0' 的时候并不会停下来
  • 如果sourcedestination有任何的重叠,复制的结果都是未定义的

模拟实现:

#include<stdio.h>
#include<string.h>
#include<assert.h>

void* my_memcpy(void* dest,const void* src,size_t num)
{
	void* ret=dest;
	assert(dest&&src);
	while(num--)
	{
		*(char*)dest=*(char*)src;
		dest=(char*)dest+1;
		src=(char*)src+1;
	}
	return ret;
}

int main()
{
	int arr1[]={1,2,3,4,5,6,7,8,9,10};
	int arr2[10]={0};
	
	my_memcpy(arr2,arr1+2,20);
	
	return 0;
}

1.12 memmove函数

void * memmove ( void * destination, const void * source, size_t num );
  • memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
  • 如果源空间和目标空间出现重叠,就得使用memmove函数处理。

模拟实现:

#include<stdio.h>
#include<assert.h>

void* my_memmove(void* dest,const void* src,size_t num)
{
	void* ret=dest;
	assert(dest&&src);
	
	if(dest<src)
	{
		while(num--)
		{
			*(char*)dest=*(char*)src;
			dest=(char*)dest+1;
			src=(char*)src+1;
		}
	}
	else
	{
		while(num--)
		{
			*((char*)dest+num)=*((char*)src+num);
		}
	}
	return ret;
}

int main()
{
	int arr[]={1,2,3,4,5,6,7,8,9,10};
	my_memmove(arr,arr+2,20);
	for(int i=0;i<10;i++)
	{
		printf("%d ",arr[i]);
	}
	puts("");
	return 0;
}

1.13 memcmp函数

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
  • 比较从ptr1和ptr2指针开始的num个字节
  • 返回值如下:
    在这里插入图片描述
#include<stdio.h>
#include<string.h>

int main()
{
	int arr1[]={1,2,6};
	int arr2[]={1,2,5};
	int ret=memcmp(arr1,arr2,9);
	printf("%d\n",ret);
	
	return 0;
}

在这里插入图片描述

总结

本期学习了很多字符串相关的函数,包括基本用法和模拟实现,都给大家书写了出来,希望大家可以多敲一遍加深对于函数的印象

我是夏目浅石,希望和你一起学习进步,刷题无数!!!希望各位大佬能一键三连支持一下博主,hhhh~我们下期见喽

在这里插入图片描述
如果无聊的话,就来逛逛我的博客栈吧stack-frame.cn

原创不易,还希望各位大佬支持一下 \textcolor{blue}{原创不易,还希望各位大佬支持一下} 原创不易,还希望各位大佬支持一下

👍 点赞,你的认可是我创作的动力! \textcolor{9c81c1}{点赞,你的认可是我创作的动力!} 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向! \textcolor{ed7976}{收藏,你的青睐是我努力的方向!} 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富! \textcolor{98c091}{评论,你的意见是我进步的财富!} 评论,你的意见是我进步的财富!