八. 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个字符追加到目标字符串的'\0‘后面(包括'\0'),且目标字符串最后加'\0'字符。如果源字符串长度小于num,则源字符串全部copy过去,包括'\0'字符。
8.1 strncat 函数的使用
//举例 #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); printf("%s\n", str1); return 0; }
8.2 strncat的模拟实现
#include <stdio.h> #include <assert.h> char* my_strncat(char* dest, const char* src, size_t num) { char* ret = dest; assert(dest); assert(src); int len = strlen(src); int i = 0; while (*dest != '\0') { dest++; } if (num <= len) { while (i < num) { *dest++ = *src++; i++; } *dest = '\0'; return ret; } else { while (*dest++ = *src++) { ; } return ret; } } int main() { char arr1[20] = "abc\0xxxxxxxxx"; char arr2[] = "ab"; char* ret = my_strncat(arr1, arr2, 5); printf("%s\n", ret); return 0; }
九. strncmp函数的使用和模拟实现
int strncmp ( const char * str1, const char * str2, size_t num );
比较str1和str2的前num个字符,如果相等就继续往后⽐较,最多⽐较num个字⺟,如果提前发现不⼀ 样,就提前结束,str1>str2,则返回大于0的数,str1<str2,则返回小于0的数。如果num个字符都相等,就是相等返回0.
9.1 strncmp的模拟实现
#include <stdio.h> #include <assert.h> int my_strncmp(const char* str1, const char*str2, size_t num) { assert(str1); assert(str2); int i = 0; while (*str1 == *str2 && i < num) { str1++; str2++; i++; } if (i == num) { return 0; } else { return *str1-*str2; } } int main() { char arr1[10] = "abcdabswsj"; char arr2[] = "abcef"; int ret = my_strncmp(arr1, arr2, 4); printf("%d\n", ret); return 0; }
十. 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.
The matching process does not include the terminating null-characters, but it stops there.
功能:在字符串找另一个字符串
返回str2在str1中第一次出现的位置;如果st2没有出现,则返回null.
10.1 strstr的使用
#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); return 0; }
输出:
10.2 strstr的模拟实现
思路:不妨设cp指向str1字符串的首元素位置,s1等于cp,s2指向str2字符串的首元素位置,然后s1++,s2++遍历字符串开始比对;
(1)若str2被遍历完,则说明str1中有str2,返回cp;
(2) 若一次比对过程中字符不同,则str2从首字符开始(s2回到str2字符串的首元素位置),str1从上次起始位置的后一位开始(即s1=cp++),相应依次遍历比对;如此循环
若str1遍历完,未找到str2,则返回NULL。
以上思路叫暴力匹配法,在字符串匹配法(上)里面有详解。
char* my_strstr(const char* str1, const char* str2) { char* cp = (char* )str1; char* s1, * s2; if (*str2=='\0')//子串为空,返回主串首元素地址 return((char*)str1); while (*cp) { s1 = cp; s2 = (char*)str2; while (*s1 && *s2 && *s1 == *s2) { s1++; s2++; } if (*s2 == '\0') return(cp); else if (*s1 == '\0') return(NULL); else { cp++; } } return(NULL); } int main() { char s1[] = "abbbcdef"; char s2[] = "bbc"; char* ret = my_strstr(s1, s2); if (*ret != '\0') { printf("找到了,%s\n",ret); } else { printf("没有找到\n"); } return 0; }
输出:
十一. 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.6.111"; char* sep = "."; char* str = NULL; for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep)) { printf("%s\n", str); } return 0; }
输出:
十二、 strerror函数的使用
12.1 strerror函数的使用
char * strerror ( int errnum );
strerror函数的头文件<string.h>
strerror函数可以把参数部分错误码对应的错误信息的字符串地址返回来。
在不同的系统和C语⾔标准库的实现中都规定了⼀些错误码,⼀般是放在 errno.h 这个头⽂件中说明的, C语⾔程序启动的时候就会使⽤⼀个全⾯的变量errno来记录程序的当前错误码,只不过程序启动的时候errno是0,表⽰没有错误,当我们在使⽤标准库中的函数的时候发⽣了某种错误,就会将对应的错误码,存放在errno中 ,⽽⼀个错误码的数字是整数很难理解是什么意思,所以每⼀个错误码都是有对应的错误信息的。strerror函数就可以将错误对应的错误信息字符串的地址返回。
在c语言的库函数设计了一些错位码,当库函数在调用过程中发生各种错误,需要记录下来,记录的就是错误码
例如:404——>该网页不存在
我们打印⼀下0~10这些错误码对应的信息
#include <errno.h> #include <string.h> #include <stdio.h> //我们打印⼀下0~10这些错误码对应的信息 int main() { int i = 0; for (i = 0; i <= 10; i++) { printf("%s\n", strerror(i)); } return 0; }
输出:
当库函数调用失败 ,就会将对应的错误码,存放在errno
#include<string.h> #include<errno.h> int main() { FILE * pf = fopen("add.txt", "r");//打开文件,方式:读//打开成功,返回有效地址,打开失败,返回空指针 if(pf==NULL)//调用失败 { printf("打开失败的原因:%s\n", streror(errno)); } else { printf("打开成功\n"); } return 0; }
输出:
12.2 perror函数的使用
可以了解⼀下perror函数,它的作用直接将错误信息打印出来。
#include <stdio.h> #include <string.h> #include <errno.h> int main() { FILE* pf = fopen("add.c", "r"); if (pf == NULL)//打开失败 perror("错误信息");//库函数调用失败后,所产生错误码对应的错误信息 被打印出来== printf("错误信息:%s\n", strerror(errno)); return 0; }
解析:perror("错误信息");这段代码的意思相当于 printf("错误信息:%s\n", strerror(errno))。