1.strlen:得到字符串的长度
创建一个字符串,并创建指针指向字符串
char str1[] = "Hello";
char* p1 = "Hello";
char* p2 = "Hello";
这里字符串的长度是5,字符数组的长度是6,原因是'\0'也占一位
获取字符串的长度
printf("%d\n", strlen(++p1));//4
printf("%d\n", strlen(p1++));//5
printf("%d\n", strlen(str1));//5
printf("%d\n", strlen("ho\0w"));//2
printf("%d\n", strlen(""));//0
仿真strlen函数
size_t my_strlen(const char* str); //const char* 称为 字符串常量类型
size_t my_strlen(const char* str)
{
//方法1
int len;
for ( len = 0; str[len]; len++)
;
return len;
//方法2
//const char* p = str;
//while (*p)
// p++;
//return p - str;//地址差
}
2.strcpy与strncpy:字符串的复制
strcpy(目标字符串的首地址,被复制字符串的首地址)
strcpy(a, p + 1);
printf("%s\n", a);//2345
printf("%s\n", strcpy(a, "hello world"));//hello world
strcpy_s(目标字符串的首地址,缓冲长度,被复制字符串的首地址)
strcpy_s(a, 20, "hello world");//20为可用空间的长度
strncpy(目标字符串的首地址,被复制字符串的首地址,复制的长度)
printf("%s\n", strncpy(a, "123456", 4));//1234
strncpy_s(目标字符串的首地址,缓冲长度,被复制字符串的首地址,复制的长度)
strncpy_s(a, 8, "abcdef", 4);
printf("%s\n", a);//abcd
仿真字符串复制函数strcpy:
char* my_strcpy(char* dest, const char* src)
{
/*char* p = dest;
const char* q = src;
while (*q)
{
*p++ = *q++;
}
*p = '\0';*/
memcpy(dest, src, strlen(src) + 1);//+1是因为'\0'也占一位
return dest;
}
仿真字符串复制函数strncpy:
char* my_strncpy(char* dest, const char* src, unsigned num)
{
char* p_dest = dest;
const char* p_src = src;
int i = 0;
while (*p_src && i++ < num)
{
*p_dest = *p_src;
p_src++;
p_dest++;
}
*p_dest = '\0';
return dest;
}
3.strcat:字符串的拼接
char* strcat(char* destination , const char* source);
char a[50]="hello ";
char *p="hi ";
/*strcat*/
strcat(a, p);
strcat(strcat(a, " how"), " are");
printf("%s\n", a);//hello hi how are
注意:strcat追加拷贝,追加到目标空间后面,目标空间必须足够大,能容纳下要拼接的字符串的内容
char* strncat(char* destination, const char* source,size_t num);
strncat(a,"123456",3);//hello 123
仿真字符串拼接函数strcat
char* my_strcat(char* dest, const char* src);
char* my_strcat(char* dest, const char* src)
{
//1 找到尾部
char* p = dest;
while (*p) {
p++;
}
//2 从尾部复制
char* q = src;
while (*q)
{
*p++ = *q++;
}
*p = '\0';
//strcpy(dest + strlen(dest), src);
return dest;
}
//测试样例
a[0] = '\0';
my_strcat(a, p);
my_strcat(my_strcat(a, " how"), " are");
printf("%s\n", a);hi how are
4.strcmp:字符串的比较
int strcmp(const char* str1, const char* str2);
/*strcmp*/
printf("%d\n", strcmp("ABCDE", "a"));//-1
printf("%d\n", strcmp("ABCDE", "ABCDE"));//0
printf("%d\n", strcmp("ABCDe", "ABCDE"));//1
按照ASCII码值进行比较
仿真字符串比较函数strcmp:
int my_strcmp(const char* str1, const char* str2)
{
const char* p1 = str1;
const char* p2 = str2;
while (*p1 == *p2 && *p1)
{
p1++;
p2++;
}
// return *p1-*p2;
return *p1 - *p2 ? (*p1 - *p2) / abs(*p1 - *p2) : 0;
}
//测试样例
printf("%d\n", my_strcmp("ABCDE", "a"));//-1
printf("%d\n", my_strcmp("ABCDE", "ABCDE"));//0
printf("%d\n", my_strcmp("ABCDe", "ABCDE"));//1
5.strstr:字符串的匹配
const char* strstr(const char* str1, const char* str2);
搜索一个字符串在另一个字符串首次出现的位置
char longStr[] = "helloworld";
/*strstr*/
char* p = strstr(longStr, "world");
printf("%c %s", *p, p);// w world
printf("匹配的下标:%d\n", p - longStr);//匹配的下标 : 5
p = strstr(longStr, "you");
printf("%d %s\n", p, p);// 0 (null)
仿真字符串匹配函数strstr:
char* my_strstr(const char* str1, const char* str2);
char* my_strstr(const char* str1, const char* str2)
{
const char* p = str1;
const char* q = str2;
while (*p)
{
if (*p == *q)
{
q++;
p++;
if (*q == '\0')
{
return p - strlen(str2);
}
}
else
{
q = str2;
p++;
}
}
return NULL;
}