此前我们已经学过字符串的一些函数的使用方法,下面是我对一些字符串函数的理解
1.求字符串长度
strlen
2.长度不受限制的字符串函数
strcpy
strcat
strcmp
3.长度受限制的字符串函数
strncpy
strncat
strcmp
1.首先我们来介绍strlen函数
size_t strlen(const char * str);
^字符串已经'\0'作为结束标志,strlen函数返回的是'\0'前面出现的字符个数(不包括'\0')。
^参数指向的字符串必须以'\0'结束。
^注意函数的返回类型size_t是无符号的(易错)。
^学会strlen的模拟实现。
在这里我们大多数人会认为打印结果应该是<,但这里我们应该想到strlen的返回类型是size_t,我们可以转到size_t 的定义会发现,它的本质是unsigned int。
打印结果我们就会发现打印的结果也不是-3,至于打印的这个数怎么来的,就要去查看负数是怎么存放的了,这里不多讲。
接下来,我们自己来模拟实现strlen函数
2.接下来我们来看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'结束
^会将源字符串中的'\0'拷贝到目标空间
^目标空间必须足够大,以确保能存放源字符串
^目标空间必须可变
^学会模拟实现
但当p为常量字符串的时候,不可修改,这是错误的示范,还要注意的是目标空间必须足够大,以确保能放下源字符串
再来看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'结束
^目标空间必须足够的大,能容纳下源字符串的内容
^目标空间必须可修改
^字符串自己给自己追加,如何?
模拟实现
那能否自己给自己追加呢?因为使用的是同一个地址,再追加过程中,源头始终追不上目标函数,会陷入死循环
最后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的数字
^那么如何判断两个字符串呢?
由结果不难看出,strcmp比较的是字符串相应位置字符的ascii码值的大小
模拟实现
3.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个
num大于源字符串长度时
拷贝完源字符串后在目标空间追加'\0',直到num个
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.
这里值得注意的是strncat从'/0'开始追加,并且以'\0'结尾 ,见调试结果
自己追加自己
strncmp函数
int strncmp ( const char * str1, const char * str2, size_t num ); Compares up to num characters of the C string str1 to those of the C string 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, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.
strstr函数
const char * strstr ( const char * str1, const char * str2 ); char * strstr ( 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.
自己实现strstr函数
#includ<stdio.h>
#include<string.h>
char* my_strstr(const char* str1, const char* str2)
{
char* s1 = str1;
char* s2 = str2;
char* p = str1;
while (*p)
{
str1 = p;//每次循环进去后str1向后移动一位
while (*str1 != '\0' && *str2 != '\0' && *str1 == *str2)
{
str1++;
str2++;
}
if (*str2 == '\0')
{
return p;//找到了
}
p++;
}
return NULL;//找不到
}
int main()
{
char arr1[] = "abcdef";
char arr2[] = "bcd";
char* p = my_strstr(arr1, arr2);
printf("%s", p);
return 0;
}