本文主要简单整理了C语言中处理字符、字符串的库函数
目录
1、strlen函数
2、strcpy函数
3、strcat函数
4、strcmp函数
5、strstr函数
6、strtok函数
7、strerror函数
size_t strlen ( const char * str );
- 字符串已经‘\0’作为结束标志,strlen函数返回的是在字符串中'\0'前面出现的字符个数(不包含‘\0').
- 参数指向的字符串必须要以' \0' 结束。
- 注意函数的返回值为size_t,是无符号的(易错)
- 学会strlen函数的模拟实现
模拟实现strlen
#include
int my_strlen( const char* str)
{int count = 0;assert(str != NULL);while(*str != '\0'){count++;*str++;}return count;
}
int main()
{char arr[] = "abc";int len = my_strlen(arr);printf("%d\n",len);return 0;
}
char* strcpy(char * destination,const char * source );
- Copies the C string pointed by source into the array pointed by destination, including the terminating nullcharacter (and stopping at that point).
- 源字符串必须以以’\0'结束。
- 会将源字符串中的'\0'拷贝到目标空间。
- 目标空间必须足够大,以确保能存放源字符串。
- 目标空间必须可变。
- 学会模拟实现
举例
#include
int main()
{char arr[] = "##########";char * p = "hello";strcpy(arr,"hello");char arr2[] = {'a','b','c'}; // 没有'\0'结尾,所以输出错误strcpy(arr,arr2);printf("%s\n",arr);return 0;}
char * strcat ( char * destination, const char * source );
- Appends a copy of the source string to the destination string. The terminating null character indestination is overwritten by the first character of source, and a null-character is included at the end ofthe new string formed by the concatenation of both in destination.
- 源字符串必须以’\0‘结束。
- 目标空间必须有足够的大,能容纳下源字符串的内容。
- 目标空间必须可修改。
- 字符串自己不能给自己追加,因为‘\0’的位置改变了,找不到'\0'的位置了
举例
#include
int main()
{char arr1[20] = "hello";char arr2[] = " world";strcat(arr1,arr2);printf("%s\n",arr1);return 0;
}
自己实现strcat函数
#include
char* my_strcat(char* dest,char* src)
{char* ret = dest;assert(dest && src);// - 1.找到目标字符串的‘\0’while(*dest){*dest++;}// - 2.追加字符串包含'\0'while(*dest++ = *src++){;}return ret;
}
int main()
{char arr1[20] = "hello";char arr2[] = " world";printf("%s\n",my_strcat(arr1,arr2));return 0;
}
strcmpr * str1, const char * str2 );
- This function starts comparing the first character of each string. lf they are equal to each other, itcontinues with the following pairs until the characters differ or until a terminating null-character isreached.
标准规定
- 第一个字符串大于第二个字符串,则返回大于0的数字
- 第一个字符串等于第二个字符串,则返回0
- 第一个字符串小于第二个字符串,则返回小于0的数字
举例
#include
int main()
{char* p = "abcdef";char* q = "abbb";int ret = strcmp(p,q);if(ret<0){printf("p < q\n");}else if(ret>0){printf("p >q \n");}else{printf("p = q\n");}return 0;
}
模拟实现strcmp函数
int my_strcmp(const char* s1,const char* s2)
{while(*s1 == *s2){if(*s1 == '\0'){return 0;}s1++;s2++;}if(*s1<*s2){return -1;}else{return 1;}
}
int main()
{char* p = "abcdef";char* q = "abbb";int ret = my_strcmp(p,q); if(ret<0){printf("p < q\n");}else if(ret>0){printf("p > q \n");}else{printf("p == q\n");}return 0;
}
也可以优化一下
int my_strcmp(const char* s1,const char* s2)
{while(*s1 == *s2){if(*s1 == '\0'){return 0;}s1++;s2++;}return *s1 - *s2;
}
int main()
{char* p = "abcdef";char* q = "abbb";int ret = my_strcmp(p,q); if(ret<0){printf("p < q\n");}else if(ret>0){printf("p > q \n");}else{printf("p == q\n");}return 0;
}
char * strstr ( const char *!const char *
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
举例
#include
int main()
{char arr1[] = "abcdefabcdef";char arr2[] = "bcd";//在arr1中查找是否包换arr2数组char* ret = strstr(arr1,arr2);if(ret == NULL){printf("不存在\n");}else{printf("找到了在:%s\n",ret);}return 0;
}
模拟实现strstr函数
#include
char* my_strstr(const char* str1,const char* str2)
{assert(str1 && str2);const char* s1 = NULL;const char* s2 = NULL;const char* cp = str1;if(*str2 == '\0'){return (char*)str1;}while(*cp){s1 = cp;s2 = str2;while(*s1 && *s2 && (*s1 == *s2)){s1++;s2++;}if(*s2 == '\0'){return (char*)cp;}cp++;}return NULL;}
int main()
{char arr1[] = "abcdefabcdef";char arr2[] = "bcd";//在arr1中查找是否包换arr2数组char* ret = my_strstr(arr1,arr2);if(ret == NULL){printf("不存在\n");}else{printf("找到了在:%s\n",ret);}return 0;
}
char * strtok ( char * str, const char * sep );
- sep参数是个字符串,定义了用作分隔符的字符集合
- 第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。
- strtok函数找到str中的下一个标记,并将其用0 结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。
- strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置strtok函数的第一个参数为 NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
- 如果字符串中不存在更多的标记,则返回 NULL指针。
举例
#include
int main()
{char arr[] = "zpw@bitedu.tech";char* p = "@.";char tmp[20] = {0};strcpy(tmp,arr);char* ret = NULL;for(ret = strtok(tmp,p);ret != NULL;ret =strtok(NULL,p)){printf("%s\n",ret);}return 0;
}
char * strerror ( int errnum );
返回错误码,所对应的错误信息。
举例:
#include
#include
int main()
{FILE* pf = fopen("test.txt","r");if(pf == NULL){printf("%s\n",strerror(errno));return 1;}fclose(pf);pf = NULL;return 0;
}
上一篇:Python 海象运算符