字符串函数+内存函数介绍及模拟实现---C语言


前言

本章重点

重点介绍处理字符和字符串的库函数的使用和注意事项

  • 求字符串长度
  • strlen
  • 长度不受限制的字符串函数
  • strcpy
  • strcat
  • strcmp
  • 长度受限制的字符串函数介绍
  • strncpy
  • strncat
  • strncmp
  • 字符串查找
  • strstr
  • strtok
  • 错误信息报告
  • strerror
  • 内存操作函数
  • memcpy
  • memmove
  • memset
  • memcmp

C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在 常量字符串 中或者 字符数组 中。
字符串常量 适用于那些对它不做修改的字符串函数.


一、函数介绍

1.strlen

size_t strlen(const char * str);
  • 字符串已经 ‘\0’ 作为结束标志,strlen函数返回的是在字符串中 ‘\0’ 前面出现的字符个数(不包 含 ‘\0’ )。
  • 参数指向的字符串必须要以 ‘\0’ 结束。
  • 注意函数的返回值为size_t,是无符号的( 易错 )
  • 学会strlen函数的模拟实现
#include <stdio.h>
#include<string.h>
int main()
{
 	const char*str1 = "abcdef";
 	const char*str2 = "bbb";
 	if(strlen(str2)-strlen(str1)>0)//注:strlen的返回值是size_t类型,是无符号数,两个无符号数相减,结果也是无符号数
 	{
 		printf("str2>str1\n");
 	} 
 	else
 	{
 		printf("srt1>str2\n");
 	}
 	return 0; 
 }

2.strcpy

char* strcpy(char * destination, const char * source);
  • Copies the C string pointed by source into the array pointed bydestination, including the terminating null character (and stoppingat that point).
  • 源字符串必须以 ‘\0’ 结束。
  • 会将源字符串中的 ‘\0’ 拷贝到目标空间。
  • 目标空间必须足够大,以确保能存放源字符串。
  • 目标空间必须可变。
  • 学会模拟实现。

3.strcat

char * strcat ( char * destination, const char * source );
  • 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.
  • 源字符串必须以 ‘\0’ 结束。
  • 目标空间必须有足够的大,能容纳下源字符串的内容。
  • 目标空间必须可修改。
  • 字符串自己给自己追加,如何?
    注:strcat不能实现字符串自己给自己追加,会陷入死循环。

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 each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
  • 标准规定:
    第一个字符串大于第二个字符串,则返回大于0的数字
    第一个字符串等于第二个字符串,则返回0
    第一个字符串小于第二个字符串,则返回小于0的数字

字符串之间的比较是按一个字符一个字符进行比较,ASCII码大的字符大

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

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 terminating null-character is copied.
  • 追加num个字符到目标字符串后
#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;
}

7.strncmp

int strncmp(const char * str1, const char * str2, size_t num);
  • 比较到出现另个字符不一样或者一个字符串结束或者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; 
}

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.
  • 在str1字符串中查找str2字符串,找到了返回str1中str2所在的起始位置,没找到返回NULL
#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  printf("%s\n", pch); //打印: simple string
  return 0;
}

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* p = "ldf@992249237.com";
	const char* sep = "@.";//由@和.将字符串分割开来
	char arr[30];
	char* str = NULL;
	strcpy(arr, p);//将数据拷贝一份,处理arr数组的内容
	for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
	{
		printf("%s\n", str);
	}
}

在这里插入图片描述

10.strerror

char * strerror(int errnum);
  • 返回错误码,所对应的错误信息。
  • C语言的库函数在执行失败的时候都会有一个错误码,存放在全局变量errno中,errno是C语言内置的全局变量
#include<stdio.h>
#include<string.h>
int main()
{
	printf("%s\n", strerror(0));
	printf("%s\n", strerror(1));
	printf("%s\n", strerror(2));
	printf("%s\n", strerror(3));
	printf("%s\n", strerror(4));
	printf("%s\n", strerror(5));
	return 0;
}

在这里插入图片描述

字符分类函数:

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

11.memcpy

void * memcpy(void * destination, const void * source, size_t num);
  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
  • 这个函数在遇到 ‘\0’ 的时候并不会停下来。
  • 如果source和destination有任何的重叠,复制的结果都是未定义的。
#include<stdio.h>
#include<memory.h>
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7 };
	int arr2[10] = { 0 };
	//拷贝arr1的内容到arr2中
	memcpy(arr2, arr1, sizeof(arr1));
	return 0;
}

在这里插入图片描述

12.memmove

void * memmove(void * destination, const void * source, size_t num);
  • 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
  • 如果源空间和目标空间出现重叠,就得使用memmove函数处理。
#include<stdio.h>
#include<memory.h>
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7 ,8,9,10};
	//将1 2 3 4 5,放到3 4 5 6 7位置上
	memmove(arr1+2, arr1, 20);
	return 0;
}

在这里插入图片描述

13.memcmp

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

二、模拟实现

1.模拟实现strlen

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

2.模拟实现strcpy

#include<stdio.h>
#include<assert.h>
char* my_strcpy(char* des, const char* src)
{
	assert(src && des);
	char* ret = des;
	while (*des++ = *src++)
	{
		;
	}
	return ret;
}
int main()
{
	char arr[] = "abcdef";
	char str[20] = { 0 };
	my_strcpy(str, arr);
	printf("%s\n", str);
	return 0;
}

3.模拟实现strcat

#include<stdio.h>
#include<assert.h>
char* My_strcat(char* des, const char* src)
{
	assert(des && src);
	char* ret = des;
	while (*des != '\0')
	{
		des++;
	}
	while (*des++ = *src++)
	{
		;
	}
	return ret;
}
int main()
{
	char arr[20] = { "Hello " };
	My_strcat(arr, "World");
	printf("%s\n", arr);
	return 0;
}

4.模拟实现strstr

#include<stdio.h>
#include<assert.h>
char* My_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	const char* s1 = str1;
	const char* s2 = str2;
	const char* p = NULL;
	while (*s1 != '\0')
	{
		if (*s1 == *s2)
		{
			p = s1;
			while (*s1 == *s2)
			{
				s1++;
				s2++;
				if (*s2 == '\0')
					return (char*)p;
				if (*s1 == '\0')
					return NULL;
			}
			s2 = str2;
			s1 = p;
		}
		s1++;
	}
	return NULL;
}
int main()
{
	char arr1[] = "abbbcdef";
	char arr2[] = "bbc";
	char* ret = My_strstr(arr1, arr2);
	printf("%s\n", ret);
	return 0;
}

5.模拟实现strcmp

#include<stdio.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++;
	}
	return *str1 - *str2;
}
int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abcdg";
	int ret = My_strcmp(arr1, arr2);
	printf("%d\n", ret);
	return 0;
}

6.模拟实现memcpy

void* My_memcpy(void* des, const void* src, size_t num)
{
	assert(des && src);
	void* ret = des;
	while (num)
	{
		*(char*)des = *(char*)src;
		des=(char*)des+1;
		src=(char*)src+1;
		num--;
	}
	return ret;
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7 };
	int arr2[10] = { 0 };
	My_memcpy(arr2, arr1, sizeof(arr1));
	return 0;
}

7.模拟实现memmove

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void* My_memmove(void* des, const void* src, size_t num)
{
	assert(des && src);
	char* mal = (char*)malloc(num);
	if (mal == NULL) 
		exit(1);
	char* ret = mal;
	void* dest = des;
	size_t n = num;
	while (n)
	{
		*mal = *(char*)src;
		mal++;
		src = (char*)src + 1;
		n--;
	}
	mal = ret;
	n = num;
	while (n)
	{
		*(char*)des = *mal;
		des = (char*)des + 1;
		mal++;
		n--;
	}
	free(ret);
	return dest;
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	My_memmove(arr1 + 2, arr1, 20);
	return 0;
}

总结

以上就是关于字符串函数和内存函数的介绍,及相关函数的实现。希望对大家有所帮助

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值