字符函数和字符串函数(2)

张开发
2026/4/20 9:50:11 15 分钟阅读

分享文章

字符函数和字符串函数(2)
个人主页深邃-❄️专栏传送门《C语言》《数据结构》Gitee仓库《C语言》《数据结构》目录strncpy函数的使用strncat函数的使用strncmp函数的使用Return Valuestrstr的使用和模拟实现strstr的使用strstr的模拟实现strtok函数的使用strerror函数的使用perror函数strncpy函数的使用strncpy的超链char*strncpy(char*destination,constchar*source,size_tnum);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个。注意strncpy 按照n的个数拷贝覆盖必须带\0//strncpy 按照n的个数拷贝覆盖必须带\0#includestdio.h#includestring.hintmain(){//strncpychararr1[10]abc\0defgh;chararr2[20]xxxxxxxxxxx;chararr3[20]xxxxxxxxxxx;strncpy(arr2,arr1,5);strncpy(arr3,arr1,2);printf(%s\n,arr2);printf(%s\n,arr3);//strcpychararr4[10]abc\0defgh;chararr5[20]xxxxxxxxxxx;strcpy(arr5,arr4);printf(%s\n,arr5);chararr6[10]{a,b};chararr7[20]xxxxxxxxxxx;strcpy(arr7,arr6);printf(%s\n,arr7);return0;}strncat函数的使用strncat的超链char*strncat(char*destination,constchar*source,size_tnum);Appends the first num characters of source to destination, plus a terminating null-character.将source指向字符串的前num个字符追加到destination指向的字符串末尾再追加一个\0字符。If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.如果source 指向的字符串的长度小于num的时候只会将字符串中到\0的内容追加到destination指向的字符串末尾。#includestdio.h#includestring.hintmain(){chararr1[10]abc;chararr2[20]xxxxx\0xxxxxxxxx;strncat(arr2,arr1,6);printf(%s\n,arr2);strncat(arr2,arr1,2);//strncat会自动添加\0printf(%s\n,arr2);return0;}/* strncat example */#includestdio.h#includestring.hintmain(){charstr1[20];charstr2[20];strcpy(str1,To be );strcpy(str2,or not to be);strncat(str1,str2,6);printf(%s\n,str1);return0;}strncmp函数的使用strncmp的超链intstrncmp(constchar*str1,constchar*str2,size_tnum);科技蓝色比较str1和str2的前num个字符如果相等就继续往后比较最多比较num个字母如果提前发现不一样就提前结束大的字符所在的字符串大于另外一个。如果num个字符都相等就是相等返回0.Return ValueReturns an integral value indicating the relationship between the strings:return valueindicates0the first character that does not match has a lower value instr1than instr20the contents of both strings are equal0the first character that does not match has a greater value instr1than instr2#includestdio.h#includestring.hintmain(){chararr1[10]abcqw;chararr2[20]abcdef;intrstrncmp(arr2,arr1,3);if(r0)printf(\n);elseif(r0)printf(\n);elseprintf(\n);return0;}strstr的使用和模拟实现strstr的超链strstr的使用char*strstr(constchar*str1,constchar*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. 字符串的比较匹配不包含 \0 字符以 \0 作为结束标志。#includestdio.h#includestring.hintmain(){chararr1[]heheabcdefabcdef;chararr2[]deq;char*pstrstr(arr1,arr2);if(p!NULL){printf(找到了, %s\n,p);}else{printf(找不到\n);}return0;}/* strstr example */#includestdio.h#includestring.hintmain(){charstr[]This is a simple string;char*pch;pchstrstr(str,simple);//返回匹配的地址strncpy(pch,sample,6);printf(%s\n,str);return0;}strstr的模拟实现#includestdio.h#includestring.h#includeassert.h//暴力写法char*my_strstr(constchar*str1,constchar*str2){assert(str1str2);constchar*pstr1;constchar*s1NULL;constchar*s2NULL;//特殊的场景的处理if(*str2\0)return(char*)str1;while(*p)//枚举查找的次数{s1p;s2str2;//找一次的匹配过程while(*s1*s2*s1*s2){s1;s2;}if(*s2\0)return(char*)p;p;}returnNULL;}intmain(){/*char arr1[] heheabcdefabcdef; char arr2[] deq;*/chararr1[]abbbcdef;chararr2[]bbc;char*pmy_strstr(arr1,arr2);if(p!NULL){printf(找到了, %s\n,p);}else{printf(找不到\n);}return0;}strtok函数的使用strtok的超链char*strtok(char*str,constchar*sep);sep参数指向一个字符串定义了用作分隔符的字符集合第一个参数指定一个字符串它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。strtok函数找到str中的下一个标记并将其用 \0 结尾返回一个指向这个标记的指针。注strtok函数会改变被操作的字符串所以被strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。strtok函数的第一个参数不为NULL函数将找到str中第一个标记strtok函数将保存它在字符串中的位置。strtok函数的第一个参数为NULL函数将在同一个字符串中被保存的位置开始查找下一个标记。如果字符串中不存在更多的标记则返回NULL指针。#includestdio.h#includestring.h#includeassert.hintmain(){chararr[]zpengweiyeah.net.hehe.hahaheihei;charsep[].;charbuf[200]{0};strcpy(buf,arr);char*pNULL;for(pstrtok(buf,sep);p!NULL;pstrtok(NULL,sep)){printf(%s\n,p);}return0;}strerror函数的使用char*strerror(interrnum);strerror 函数可以把参数部分错误码对应的错误信息的字符串地址返回来。在不同的系统和C语言标准库的实现中都规定了一些错误码一般是放在 errno.h 这个头文件中说明的。C语言程序启动的时候就会使用一个全局的变量 errno来记录程序的当前错误码只不过程序启动的时候errno是0表示没有错误。当我们在使用标准库中的函数的时候发生了某种错误就会将对应的错误码存放在 errno 中。而一个错误码的数字是整数很难理解是什么意思所以每一个错误码都是有对应的错误信息的。strerror函数就可以将错误对应的错误信息字符串的地址返回。#includestdio.h#includestring.h#includeassert.h#includeerrno.hintmain(){inti0;for(i0;i10;i){printf(%d: %s\n,i,strerror(i));}return0;}C语言可以打开文件fopen如果以读的形式打开文件文件是必须要存在的如果文件不存在则打开文件失败fopen函数就会将错误码放在errno同时函数会返回NULL#includestdio.h#includestring.h#includeassert.h#includeerrno.hintmain(){//C语言可以打开文件//fopen//如果以读的形式打开文件文件是必须要存在的如果文件不存在则打开文件失败//fopen函数就会将错误码放在errno//同时函数会返回NULLFILE*pffopen(data.txt,r);if(pfNULL){printf(%s\n,strerror(errno));return1;}//读文件//......//关闭文件fclose(pf);pfNULL;return0;}perror函数p e r r o r p r i n t f s t r e r r o r perror printf strerrorperrorprintfstrerror#includestdio.h#includestring.h#includeerrno.hintmain(){FILE*pFile;pFilefopen(unexist.ent,r);if(pFileNULL)perror(Error opening file unexist.ent);return0;}也可以了解一下 perror 函数perror函数相当于上一次将上述代码中的第9行完成了直接将错误信息打印出来。perror函数打印完参数部分的字符串后再打印一个冒号和一个空格再打印错误信息perror(你可以自己标注出错误的内容)接下来可以在动态内存管理遇见perror,点击跳转–动态内存管理

更多文章