一定义:
字符串:字符串是由零个或者多个字符组成的有限串行;
子串:字符串中任意个连续的字符组成的子序列,并规定空串是任意串的子串,字符串本身也是子串之一;“abcdefg”,”abc“就是其子串,但是“ade”不属于子串范围。
子序列:不要求字符连续,但是其顺序与其在主串中相一致;上例中,“abc”与“ade”都属于子序列范围;
二:C风格字符串包括两种:
1)字符串常量---以双引号括起来的字符序列,编译器自动在其末尾添加一个空字符。
2)末尾添加了’0‘的字符数组;
1 2 3 4 5 |
|
三:标准库提供的字符串处理函数:
1 2 3 4 |
|
注意:
1、自定义str库函数时,首先要明确接收的参数是否为空(assert),这样可有效避免bug;
2、对函数的参数要尽量多的应用const,以避免无意间修改了字符串。
3、要自行添加字符串的结束符‘\0’。
1)自定义实现strlen函数的功能;
int strlen(const char* src)
3 {
4 assert(str != NULL);
5 int count =0;
6
7 while(*str++ != '\0')
8 count++;
9 return count;
10 }
2)自定义实现strcat函数的功能;
char* strcat(char *strD, const char *strS)
{
assert(strD != NULL && strS != NULL);
char* address = strD;
while(*strD != '\0')//走到末尾
strD ++;
while(*strD++ = *strS++);//cat
return address; //attention
}
3)自定义实现strcmp函数的功能;
int strcmp(const char *s1, const char *s2)
{
assert(s1 != NULL && s2 != NULL);
int ret;
s1 ="abcd"; s2 = "abcde";则ret = 0-'e' < 0
while( !(ret = *(unsigned char*)s1 - *(unsigned char*)s2) && *s1)
{
s1 ++;
s2 ++;
}
if(ret < 0) return -1;
else if( ret >0) return 1;
return 0;
}
4)自定义实现strcpy函数的功能;
char* strcpy(char *strD, const char *strS)
{
assert(strD != NULL && strS != NULL);
char* address = strD ;
//attention此处,若*strS为’\0‘,则其先赋值给strD,故最后不需要再添加'\0'
while(*strD++ = *strS++);
return address;
}
测试函数如下:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(int argc, char const *argv[])
{
char s1[] = "helloworld";
printf("%d\n", s1);
char s2[100] = "thank you";
printf("%s\n", strcat(s2,s1));
printf("%d\n", strcmp(s2, s1));
printf("%s\n", strcpy(s2, s1));
return 0;
}