各位CSDN的uu们你们好呀,今天小雅兰来给大家介绍一个全新的知识点,就是字符函数和字符串函数啦,其实其中有些函数我之前已经学习过了,比如strlen、strcpy;也有一些之前不是很熟悉的函数,比如strstr、strtok、strerror等等。话不多说啦,现在,让我们进入字符函数和字符串函数的世界吧
求字符串长度
strlen
长度不受限制的字符串函数
strcpy
strcat
strcmp
长度受限制的字符串函数介绍
strncpy
strncat
strncmp
C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在
常量字符串中或者字符数组中。
字符串常量适用于那些对它不做修改的字符串函数.
strlen
size_t strlen ( const char * str );
字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> int main() { char arr[] = "abc\0def"; int len = strlen(arr); printf("%d\n", len); return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> int main() { char arr[] = "abcdef"; int len = strlen(arr); printf("%d\n", len); return 0; }
abcdef后面隐藏了一个\0
参数指向的字符串必须要以 '\0' 结束。
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> int main() { char arr[3] = { 'a','b','c'}; int len = strlen(arr); printf("%d\n", len); return 0; }
如果字符串不以\0结束,那么,结果就是一个随机值
注意函数的返回值为size_t,是无符号的
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> int main() { const char* str1 = "abcdef"; const char* str2 = "bbb"; if (strlen(str2) - strlen(str1) > 0) { printf("str2>str1\n"); } else { printf("srt1>str2\n"); } return 0; }
模拟实现strlen
三种方式:
1.计数器的方式
2.递归的方式
3.指针-指针的方式
函数递归+青蛙跳台阶——“C”_认真学习的小雅兰.的博客-CSDN博客
指针——“C”_认真学习的小雅兰.的博客-CSDN博客
1.计数器的方式
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> int my_strlen(const char* str) { //计数器的方式 int count = 0; assert(str != NULL); while (*str != '\0') { str++; count++; } return count; } int main() { char arr[] = "abcdef"; int len = my_strlen(arr); printf("%d\n", len); return 0; }
2.递归的方式
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> //不能创建临时变量,求字符串的长度 int my_strlen(const char * str) { if(*str=='\0') return 0; else return 1 + my_strlen(str+1); } int main() { char arr[] = "abcdef"; int len = my_strlen(arr); printf("%d\n", len); return 0; }
3.指针-指针的方式
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> //指针-指针的方式 int my_strlen(char * s) { char * p = s; while( * p != '\0') p++; return p-s; } int main() { char arr[] = "abcdef"; int len = my_strlen(arr); printf("%d\n", len); return 0; }
strcpy
char* strcpy(char * destination, const char * source );
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
源字符串必须以 '\0' 结束。
#include<stdio.h> #include<string.h> int main() { char arr1[3] = { 'a','b','c' }; char arr2[20] = "xxxxxx"; strcpy(arr2, arr1); printf("%s\n", arr2); return 0; }
这样程序直接崩溃了
会将源字符串中的 '\0' 拷贝到目标空间。
目标空间必须足够大,以确保能存放源字符串。
#include<stdio.h> #include<string.h> int main() { //错误的示范 char arr1[20] = "abcdefghi"; char arr2[3] = ""; strcpy(arr2, arr1); printf("%s\n", arr2); return 0; }
目标空间必须可变。
#include<stdio.h> #include<string.h> int main() { //错误的示范 char* p = "abcdefghi"; char arr2[20] = "hehe"; strcpy(p, arr2); printf("%s\n", p); return 0; }
模拟实现strcpy
#include<stdio.h> #include<string.h> //1.参数顺序 //2.函数的功能,停止条件 //3.assert //4.const修饰指针 //5.函数返回值 //6.题目出自《高质量C/C++编程》书籍最后的试题部分 //返回的是目标空间的起始地址 #include<assert.h> char * my_strcpy(char * dest, const char* src) { char * ret = dest; assert(dest!=NULL); assert(src != NULL); while ((*dest++ = *src++)) { ; } return ret; } int main() { char arr1[] = "hehe"; char arr2[20] = { 0 }; my_strcpy(arr2, arr1); printf("%s\n", arr2); return 0; }
strcat
char * strcat ( char * destination, const char * source );
#include<stdio.h> #include<string.h> int main() { char arr1[20] = "hello "; char arr2[] = "world"; //追加 strcat(arr1, arr2); printf("%s\n", arr1); return 0; }
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' 结束。
#include<stdio.h> #include<string.h> int main() { char arr1[20] = "hello \0xxxxxxxxxxxx"; char arr2[] = "world"; //追加 strcat(arr1, arr2); printf("%s\n", arr1); return 0; }
目标空间必须有足够的大,能容纳下源字符串的内容。
目标空间必须可修改。
模拟实现strcat
#include<stdio.h> #include<assert.h> char* my_strcat(char* dest, const char* src) { char* ret = dest; assert(dest != NULL); assert(src != NULL); //找目标空间的\0 while (*dest!='\0') { dest++; } //拷贝 while ((*dest++ = *src++)) { ; } return ret; } int main() { char arr1[20] = "hello "; char arr2[] = "world"; //追加 my_strcat(arr1, arr2); printf("%s\n", arr1); return 0; }
绝对不能自己给自己追加!!!
#include<stdio.h> #include<assert.h> char* my_strcat(char* dest, const char* src) { char* ret = dest; assert(dest != NULL); assert(src != NULL); //找目标空间的\0 while (*dest != '\0') { dest++; } //拷贝 while ((*dest++ = *src++)) { ; } return ret; } int main() { char arr1[20] = "bit"; //追加 my_strcat(arr1, arr1); printf("%s\n", arr1); return 0; }
strcmp
“abcdef"=="bbcdef",这里比较的是两个字符串首字符的地址,而并不是字符串的内容
比较两个字符串内容的时候,不能使用==,应该使用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的数字
#include<stdio.h> #include<string.h> #include<assert.h> int main() { char arr1[] = "abcdef"; char arr2[] = "bbcdef"; int ret = strcmp(arr1, arr2); printf("%d\n", ret); return 0; }
模拟实现strcmp
#include<stdio.h> #include<string.h> #include<assert.h> int my_strcmp(const char* str1, const char* str2) { assert(str1 != NULL); assert(str2 != NULL); while (*str1 == *str2) { if (*str1 == '\0') { return 0; } str1++; str2++; } if (*str1 > *str2) { return 1; } else { return -1; } } int main() { char arr1[] = "abcdef"; char arr2[] = "bbcdef"; int ret = my_strcmp(arr1, arr2); printf("%d\n", ret); return 0; }
另一种写法:
#include<stdio.h> #include<string.h> #include<assert.h> int my_strcmp(const char* str1, const char* str2) { assert(str1 != NULL); assert(str2 != NULL); while (*str1 == *str2) { if (*str1 == '\0') { return 0; } str1++; str2++; } return *str1 - *str2; } int main() { char arr1[] = "abcdef"; char arr2[] = "bbcdef"; int ret = my_strcmp(arr1, arr2); printf("%d\n", ret); return 0; }
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<stdio.h> #include<string.h> #include<assert.h> int main() { char arr1[] = "abcdef"; char arr2[5] = { 0 }; strncpy(arr2, arr1, 3); printf("%s\n", arr2); return 0; }
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.
#include<stdio.h> #include<string.h> #include<assert.h> int main() { char arr1[20] = "hello \0xxxxxxxx"; char arr2[] = "world"; strncat(arr1, arr2, 3); printf("%s\n", arr1); return 0; }
strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。
#include<stdio.h> #include<string.h> #include<assert.h> int main() { char arr1[] = "abcdef"; char arr2[] = "abcq"; int ret = strncmp(arr1, arr2, 4); printf("%d\n", ret); return 0; }
好啦,小雅兰今天的内容就到这里啦,还要继续加油呀!!!