C语言字符类函数
发布日期:2021-05-07 23:09:48 浏览次数:16 分类:精选文章

本文共 4929 字,大约阅读时间需要 16 分钟。

C语言中字符类函数:

strcpy()函数
strlen()函数
memmove()函数
strstr()函数
strcat()函数
memcpy()函数

一、strcpy()函数

函数原型:char *strcpy(char *des,const char *source);
头文件#include<string.h>
目的:将source中的字符拷贝到des中。
注意:1.必须要保证des的空间足够大,要不会出现内存越界的问题。
2.必须将源字符串source声明为const,避免对源字符串的错误修改。
函数实现代码:

#define _CRT_SECURE_NO_WARNINGS#include 
#include
#include
#include
#include
#include
#include
char* my_strcpy(char* des, const char* src){ assert(des != NULL && src != NULL); char* pdes = des; const char* psrc = src; while (*psrc != '\0') { *pdes = *psrc; pdes++; psrc++; } *pdes = '\0'; return des;}void test01(){ char buf[25] = "hello"; char buf1[12] = "hello world"; char* des = my_strcpy(buf, buf1); printf("%s\n", des);}int main(void){ test01(); system("pause"); return EXIT_SUCCESS;}

二.strlen()函数

函数原型: int strlen(const char *des);
目的:返回字符串的有效字符,除’\0’外。
特性:strlen()函数遇到’\0’停止读取,并返回字符数
与sizeof()区别:
2.1 在计算字符串长度时sizeof()会将’\0’作为有效字符,而strlen不会这样
2.2 在字符串作为函数参数时,sizeof()会将其视为一个指针。在32位下为4个字节,64位为8个字节。strlen不会出现这样情况,仍将其视为字符串处理。返回其有效字符数
strlen()代码实现:

#define _CRT_SECURE_NO_WARNINGS#include 
#include
#include
#include
#include
#include
#include
int my_strlen(const char* des){ assert(des != NULL); const char* pdes = des; int len = 0; while (*pdes++ != '\0') { len++; } return len;}int digit_mystrlen( char* des){ assert(des != NULL); if (*des == '\0') { return 0; } return 1 + digit_mystrlen(des + 1);}void test01(){ char* des = "hello world"; //int len = my_strlen(des); int len = digit_mystrlen(des);//递归 printf("%s\t的有效位数为%d\n", des, len);}int main(void){ test01(); system("pause"); return EXIT_SUCCESS;}

三、strcat()函数

函数原型:char *strcat(char *des,const char *src);
目的:是将src的值附在des后面,从des的’\0’位置开始
注意:必须保证des足够大,足以容纳自身大小+src大小值。
strcat()函数实现:

#define _CRT_SECURE_NO_WARNINGS#include 
#include
#include
#include
#include
#include
#include
char* my_strcat(char* dest, const char* src){ assert(dest != NULL && src != NULL); char* pdest = dest; while (*pdest != '\0') { pdest++; } while (*src != '\0') { *pdest++ = *src++; } *pdest = '\0'; return dest;}void test01(){ char buf[25] = "hello"; char buf1[10] = " world"; char* des = my_strcat(buf, buf1); printf("%s\n", des);}int main(void){ test01(); system("pause"); return EXIT_SUCCESS;}

四、strstr()函数

函数原型char *strstr(char *str,const char *src);
目的:返回在src中与str字符匹配的首元素地址
若含有则返回相应的地址,若不含有,则返回NULL值

#define _CRT_SECURE_NO_WARNINGS#include 
#include
#include
#include
#include
#include
#include
//char* my_strstr(const char* str1, const char* str2)//{ // assert(str1 != NULL && str2 != NULL);// int len = strlen(str1);// char* des = str1;// char* source = str2;// while (*source != '\0')// { // if (strncmp(des, source, len)==0)// { // return source;// }// else// { // source += 1;// }// }// return NULL;//}char* my_strstr(const char* str1, const char* str2){ assert(str1 != NULL && str2 != NULL); const char* pstr1 = str1; const char* pstr2 = str2; int i, j; i = 0; j = 0; while (pstr1[i] != '\0' && pstr2[j] != '\0') { if (pstr1[i] == pstr2[j]) { i++; j++; } else { i = i - j + 1; //关键 j = 0; } } if (pstr1[j] == '\0') return pstr1 + i - j; return NULL;}void test01(){ char* str = "hello world"; char* str1 = "hllo"; char* des = my_strstr(str1, str); if (des == NULL) { printf("未找到\n"); } else printf("str比对结果是%s\n", des);}int main(void){ test01(); system("pause"); return EXIT_SUCCESS;}

注:此种方法并不是最简单的方法,更简单的是采用kmp算法实现。

五、memcpy()函数与memmove()函数
函数原型:void*memcpy(void *des,void src,unsigned n);
void
memmove(void *des,void *src,unsigned n);
头文件#include<string.h>
目的:将src内存中的大小为n个字节的数据拷贝
memcpy()函数与memmove()函数对比:
1.在不发生内存重叠的情况下,此时memcpy()函数的效率高于memmove情况。
2.在发生内存重叠的情况时,此时memmove()可以避免内存重叠所带来的弊端,保持拷贝的正确性。而memcpy()无法避免这种情况。
函数实现,因为memmove相对于memcpy考虑了内存重叠的情况,故读者可以将memcpy()视为代码段中不含else时的代码。(这块代码是用来处理内存重叠情况的)。

#define _CRT_SECURE_NO_WARNINGS#include 
#include
#include
#include
#include
#include
#include
//void* memcpy(void* destin, void* source, unsigned n);//函数原型// 本质:逐字节进行拷贝//问题:本函数会出现内存重叠的现象,固有两中写入的办法。void* my_memcpy(void* destin, void* source, int n){ n = (int)n; assert(destin != NULL && source != NULL); char* pdes = (char*)destin; const char* psrc = (const char*)source; //没有发生内存重叠的现象 if (pdes > psrc || pdes > psrc + n) { while (n-- != 0) { *pdes++ = *psrc++; } } //当发生内存重叠的情况时,让目标空间由后向前进行拷贝,避免了内存覆盖的情况 else { pdes += n - 1; psrc += n - 1; while (n-- != 0) { *pdes = *psrc; } } return destin;}void test01(){ char buf[12] = "hello"; char buf1[10] = "world"; printf("复制前%s\n", buf); my_memcpy(buf, buf1, 3); printf("复制后%s\n", buf);}int main(void){ test01(); system("pause"); return EXIT_SUCCESS;}
上一篇:C语言字符串分割的两种方式
下一篇:C语言printf()与scanf()

发表评论

最新留言

很好
[***.229.124.182]2025年04月16日 11时36分05秒