目录:
前言 :
重点介绍处理字符和字符串的库函数的使用和注意事项
- 求字符串长度
strlen - 长度不受限制的字符串函数
strcpy
strcat
strcmp - 长度受限制的字符串函数介绍
strncpy
strncat
strncmp
注意:
C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,
字符串通常放在常量字符串中或者字符数组中。
字符串常量适用于那些对它不做修改的字符串函数.
1、 函数介绍
1.1 strlen
size_t strlen ( const char * str );
- 字符串以’\0’作为结束标志,strlen函数返回的是在字符串中’\0’前面出现的字符个数(不包含’\0’)。
- 参数指向的字符串必须要以’\0’结束。
- 注意函数的返回值为size_t,是无符号的(易错)
代码演示:
//代码1 #include<stdio.h> int main() { //a b c \0 d e f \0 //char arr[] = "abc\0def";//\0前又三个字符,结果为3 char arr[] = { 'a', 'b', 'c' }; //这里求的值是随机数, printf("%d\n", strlen(arr)); return 0; }
//代码2 #include<stdio.h> int main() { if (strlen("abc") - strlen("abcdef") > 0) printf(">\n"); else printf("<\n"); return 0; }
注意:如上代码2的结果并不是<, 而是>
因为strlen函数的默认返回值为size_t,是无符号的
- 学会strlen函数的模拟实现:
#include<assert.h> size_t my_strlen(const char* str) { assert(str);//保证str为空值 const char* start = str; const char* end = str; while (*end != '\0') { end++; } return end - start;//指向\0的指针-指向初始位置的指针,得到的是\0之前元素的个数 } int main() { char arr[] = "abcdef"; int len = my_strlen(arr); printf("%d\n", len); return 0; }
1.2 strcpy
char* strcpy(char * destination, const char * source );
示例:
int main() { char arr[10] = { 0 }; const char* p = "abcdef"; strcpy(arr, p); printf("%s\n", arr); return 0; }
- Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
- 源字符串必须以’\0’结束。
- 会将源字符串中的’\0’拷贝到目标空间。
- 目标空间必须足够大,以确保能存放源字符串。
- 目标空间必须可变,不能为常量字符串
错误示例:
int main() { char arr[10] = "xxxxxxxxx"; //const char* p = "abcdef";//错误,源字符串中必须出现\0 char arr2[] = { 'b', 'i','\0', 't'}; //strcpy(arr, p);//错误 strcpy(arr, arr2); printf("%s\n", arr); return 0; } int main() { char arr[3] = {0};//错误,目标空间必须足够大 char arr2[] = "abcdef"; strcpy(arr, arr2); printf("%s\n", arr); return 0; } int main() { char* p = "hello world";//错误,目标空间不能是常量字符串 char arr2[] = "abcdef"; strcpy(p, arr2); printf("%s\n", p); return 0; }
- 模拟实现:
//strcpy模拟实现 #include<assert.h> char* my_strcpy(char*dest,const char*src) { assert(dest); assert(src); char* ret = dest; while (*dest++ = *src++) { ; } return ret; } int main() { char arr1[20] = "abc"; char arr2[] = "hellow world"; printf("%s\n", my_strcpy(arr1, arr2)); return 0; }
1.3 strcat
- char * strcat ( char * destination, const char * source );
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination. - 源字符串必须以’\0’结束,从\0往后追加
- 目标空间必须有足够的大,能容纳下源字符串的内容。
- 目标空间必须可修改。
- 字符串自己给自己追加,如何?
阅读资料,我们可以发现strcat函数和strcpy函数的用法类似
示例:
int main() { char arr1[20] = "hellow"; char arr2[] = "world"; stract(arr1, arr2);//字符追加函数 printf("%s\n,arr1"); return 0; }
输出结果为:hello world
模拟实现:
//strcat模拟实现 char* my_strcat(char* dest, const char* src) { //1.找目标空间中的\0 char* cur = dest; while (*cur != '\0') { cur++; } //2、拷贝源头数据到\0之后的空间 while (*cur++ = *src++) { ; } return dest; } int main() { char arr1[20] = "hello \0 xxxxxxxxx"; char arr2[] = "world"; my_strcat(arr1, arr2);//字符追加函数 printf("%s\n",arr1); return 0; }
1.4 strcmp
int strcmp ( const char * str1, const char * str2 );
- This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
- 标准规定:
- 第一个字符串大于第二个字符串,则返回大于0的数字 -
- 第一个字符串等于第二个字符串,则返回0
- 第一个字符串小于第二个字符串,则返回小于0的数字
- 需要有\0
- 那么如何判断两个字符串?
示例:
//strcmp字符比较函数,比较的是每个字符的大小 int main() { //char arr1[] = "abcdef"; //char arr2[] = "abq"; //char arr1[] = "abcd"; //char arr2[] = "abc"; char arr1[] = "abc"; char arr2[] = "abc"; //char arr1[] = { 'a', 'b', 'c' };//需要有\0 //char arr2[] = { 'a', 'b', 'c' }; int ret = strcmp(arr1, arr2); if (ret < 0) printf("arr1<arr2\n"); else if(ret>0) printf("arr1>arr2\n"); else printf("arr1==arr2\n"); printf("%d\n", ret); return 0; }
模拟实现strcmp:
//int my_strcmp(const char*s1, const char*s2) //{ // assert(s1 && s2);//使s1,s2不为空 // while (*s1 == *s2) // { // if (*s1 == '\0') // { // return 0; // } // s1++; // s2++; // } // if (*s1 > *s2) // return 1; // else // return -1; //} // int my_strcmp(const char* s1, const char* s2)//比较而不希望改变加const { assert(s1 && s2); while (*s1 == *s2) { if (*s1 == '\0') { return 0; } s1++; s2++; } return *s1 - *s2; } int main() { //char arr1[] = "abcdef"; //char arr2[] = "abq"; //char arr1[] = "abcd"; //char arr2[] = "abc"; char arr1[] = "abc"; char arr2[] = "abc"; //char arr1[] = { 'a', 'b', 'c' };//需要有\0 //char arr2[] = { 'a', 'b', 'c' }; int ret = my_strcmp(arr1, arr2); if (ret < 0) printf("arr1<arr2\n"); else if (ret > 0) printf("arr1>arr2\n"); else printf("arr1==arr2\n"); printf("%d\n", ret); return 0; }
下面的函数和以上函数相似,从名称可以看出其内部多加了n
即我们可以对参数有所限制
1.5 strncpy
char * strncpy ( char * destination, const char * source, size_t num );
- 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个。
示例:
#include<string.h> int main() { char arr1[20] = "abcdefghi"; char arr2[] = "xxxx"; strncpy(arr1, arr2, 2); printf("%s\n", arr1); return 0; //运行结果为xxxxefghi }
#include<string.h> int main() { char arr1[20] = "abcdefghi";//目标空间 char arr2[] = "xxxx";//源字符串 strncpy(arr1, arr2, 8);//num>源字符串长度 printf("%s\n", arr1); return 0; //运行结果为xxxx 在目标的后边追加0,直到num个。 }
1.6 strncat
char * strncat ( char * destination, const char * source, size_t num );
- 示例
int main() { char arr1[20] = "abcdef\0qqqqqq"; char arr2[] = "xyz"; strncat(arr1, arr2, 2); printf("%s\n", arr1); return 0;//abcdefxy //从\0开始追加,追加结束后会自动补\0 }
int main() { char arr1[20] = "abc"; strncat(arr1, arr1, 3); printf("%s\n", arr1); return 0; //与strcat不同的是strncat可以给自己追加 }
1.7 strncmp
- 比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。
示例:
int main() { int ret = strncmp("abcdef", "abc", 3); printf("%d\n", ret); return 0;//0 }
int main() { int ret = strncmp("abcdef", "abc", 4); printf("%d\n", ret); return 0;//1 }
结语:
这里我们关于C】字符函数和字符串函数(上)的内容就介绍完了,文章中某些内容我们之前有介绍,所以只是一笔带过,还请谅解。希望以上内容对大家有所帮助👀,如有不足望指出🙏
加油!!