天天看点

对于字符函数和字符串函数的介绍(下),strstr,strtok,strerror,memcpy,memmove

strstr函数的介绍

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

strstr函数是在第一个字符串中查找第二个字符串是否存在.找到后将首地址返回,没找到就返回NULL.

strstr例子

#include <stdio.h>
#include <stdlib.h>
//strstr使用
int main(){
	char str1[] = "abcdefghijk";
	char str2[] = "bcde";
	char* p = NULL;
	if (p = strstr(str1, str2)){
		puts(p);
	}
	else {
		printf("没找到!\n");
	}
	system("pause");
	return 0;
}
           

strstr模拟实现

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//strstr模拟实现
char* my_strstr(char* str1, const char* str2){
	char* p = (char*)str1;
	char* s1 = (char*)str1;
	char* s2 = (char*)str2;
	assert(str1);
	if (str2 == NULL){
		return NULL;
	}
	while (*p){
		s1 = p;
		s2 = (char*)str2;
		while (*s1 && *s2 && *s1 == *s2){
			++s1;
			++s2;
		}
		if (*s2 == '\0'){
			return p;
		}
		++p;
	}
}
int main(){
	char str1[] = "abcdefggggggg";
	char str2[] = "bcdef";
	char* p = NULL;
	if (p = my_strstr(str1, str2)){
		puts(p);
	}
	else {
		printf("没找到!\n");
	}
	system("pause");
	return 0;
}
           

strtok函数的介绍

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

sep参数是个字符串,定义了分隔符的字符集合

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

例如

char str[] = "i,am.a student";
char sep[] = ",. ";
           

strtok函数找到str中的下一个标记,并将其用’\0’结尾,返回一个指向这个标记的指针.

一定要注意的是,strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可以修改.

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

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

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

strtok函数的用法

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//strtok函数的用法
int main(){
	char str[] = "i,am.a student";
	char sep[] = ",. ";
	char* tok = NULL;
	for (tok = strtok(str, sep); tok != NULL;
		tok = strtok(NULL, sep)){
		puts(tok);
	}
	system("pause");
	return 0;
}
           

strerror函数的介绍

char* strerror(int errnum);
           

strerror函数是用来返回错误码所对应对的错误信息.

strerror例子

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
//strerror例子
int main(){
	FILE* pfile;	//文件类型的指针
	pfile = fopen("unexist.ent", "r");	//打开这个文件已"r"方式(只读)
	if (pfile == NULL){
		printf("Error opening file unexist.ent : %s\n", strerror(errno));
	}
	system("pause");
	return 0;
}
           
对于字符函数和字符串函数的介绍(下),strstr,strtok,strerror,memcpy,memmove

并没有这样一个文件.

memcpy函数的介绍

void* memcpy(void* destination, const void* source, size_t num);
           

memcpy函数从source的位置开始向后复制num个字节的数据到destination的内存位置

这个函数在遇到’\0’不会停下来

如果source与destination有任何的内存重叠,复制的结果都是未定义的.

当source与destination内存重叠时需要用到memmove函数,它和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的.

记住,如果源空间和目标空间出现重叠,就得使用memmove函数处理.

接下来看memcpy与memmove函数的使用

memcpy的使用

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
	int arr1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	int arr2[10] = { 9, 8, 10 };
	//memcpy函数去处理
	memcpy(arr1 + 2, arr1, 4 * sizeof(int));
	for (int i = 0; i < 10; ++i){
		printf("%d ", arr1[i]);
	}
	printf("\n");
	system("pause");
	return 0;
}
           
对于字符函数和字符串函数的介绍(下),strstr,strtok,strerror,memcpy,memmove

同样的用memmove去处理

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
	int arr1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	//memmove函数去处理
	memmove(arr1 + 2, arr1, 6 * sizeof(int));
	for (int i = 0; i < 10; ++i){
		printf("%d ", arr1[i]);
	}
	printf("\n");
	system("pause");
	return 0;
}
           
对于字符函数和字符串函数的介绍(下),strstr,strtok,strerror,memcpy,memmove

接下来我们来看memcpy与memmove的模拟实现,从模拟实现中我们可以很清楚的看到他们的不同之处

memcpy的模拟实现

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//memcpy的模拟实现
void* my_memcpy(void* str1, const void* str2, unsigned int n){
	void* ret = str1;
	assert(str1);
	assert(str2);
	while (n){
		*(char*)str1 = *(char*)str2;
		str1 = (char*)str1 + 1;
		str2 = (char*)str2 + 1;
		--n;
	}
	return ret;
}
int main(){
	char str1[20] = "abcdefghi";
	char str2[20] = "123";
	int arr1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	int arr2[10] = { 9, 8, 10 };
	my_memcpy(str1, str2, 1);
	puts(str1);
	system("pause");
	return 0;
}
           

memmove的模拟实现

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void* my_memmove(void* str1, const void* str2, unsigned int n){
	void* ret = str1;
	if (str1 <= str2 || (char*)str2 + n <= (char*)str1){
		while (n){
			*(char*)str1 = *(char*)str2;
			str1 = (char*)str1 + 1;
			str2 = (char*)str2 + 1;
			--n;
		}
	}
	else {
		str1 = (char*)str1 + n - 1;
		str2 = (char*)str2 + n - 1;
		while (n){
			*(char*)str1 = *(char*)str2;
			str1 = (char*)str1 - 1;
			str2 = (char*)str2 - 1;
			--n;
		}
	}
	return ret;
}
int main(){
	char str1[20] = "abcdefghi";
	char str2[20] = "123";
	my_memmove(str1, str2, 1);
	puts(str1);
	system("pause");
	return 0;
}
           

继续阅读