1.strlen
作用:计算字符串长度
要点:
①只计算'\0'前面的长度
②字符串必须以'\0'结束
③返回值是无符号的
使用:
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "hello_world";
printf("%d", strlen(arr));//11
char arr1[] = { 'a','b','c', '\0'};//'\0'必须要有,不然打印错误结果
printf("\n%d", strlen(arr1));//3
char arr2[] = { 'a','\0','c', '\0' };//一旦遇到'\0'就不会看后面的字符
printf("\n%d", strlen(arr2));//1
char arr3[] = "hello_\0world";//一旦遇到\0就不会看后面的字符
printf("\n%d", strlen(arr3));//6
return 0;
}
模拟该函数
#include <stdio.h>
#include <assert.h>
int my_strlen(char* str)
{
int count = 0;
assert(str);//防止str是空指针,如果是空指针的话,逻辑为假,assert会报错
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char arr[] = "hello_world";
printf("%d", my_strlen(arr));
return 0;
}
2.strcpy
char *strcpy(char* dest, const char *src);
作用:把复制的字符串粘贴到想粘贴的目标上,即把dest的内容变成src
要点:
①目标的空间要大到能放置被复制的字符串
②被复制的字符串的结尾必须要有'\0'
③若字符串中间有'\0',以这个'\0'为结束标志
使用:
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "hello";
strcpy(arr, "world");
printf("%s", arr);//hello被改变成world
char arr1[] = "hellp";
strcpy(arr1, "wor\0ld");//hellp被改变成wor
printf("\n%s", arr1);
char arr2[2] = "he";
strcpy(arr2, "world");//world有五个,而目标只有两个空间,故出错
printf("\n%s", arr2);
return 0;
}
模拟该函数
#include <stdio.h>
#include <assert.h>
char* my_strcpy(char* dest, const char* src)
{
assert((*dest) && (src));
while (*dest++ = *src++);
return dest;
}
int main()
{
char arr[20] = "hello";//要给予大的空间以便容纳复制过来的字符串
my_strcpy(arr, ",world");
printf("%s", arr);
return 0;
}
3.strcat
char *strcat(char *dest, const char *src)
作用:追加字符串,即把src的内容加在dest的内容的后面
要点:
①目标的空间要大到能放置追加的字符串
②追加过后的字符串的结尾必须要有'\0'
③不能自己给自己追加
④若追加的字符串的中间已经有\0,以这个\0结束,不会加后面的东西
使用:
#include <stdio.h>
#include <string.h>
int main()
{
char arr[20] = "hello";
strcat(arr, ",world");
printf("%s", arr);//hello,world
char arr1[20] = "hellp";
strcat(arr1, ",wor\0ld");
printf("\n%s", arr1);//hello,wor
return 0;
}
模拟该函数
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, const char* src)
{
assert((dest) && (src));
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++);
return dest;
}
int main()
{
char arr[20] = "hello";
my_strcat(arr, ",kitty");
printf("%s", arr);
return 0;
}
4.strstr
作用:str1和str2都是字符串,该函数会检查str1的字符串里面是否会包含str2的内容,会返回第一次出现str2的地址。如果没有,则返回空指针。
char *strstr(const char *str1, const char *str2)
要点:
①因为str2是str1的子串,因此str2的长度一定要小于str1的长度
使用:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "hello_everyone";
char str2[] = ".";
char* ret = strstr(str1, str2);
if (ret == NULL)
printf("子串不存在");
else
printf("子串存在");
return 0;
}
模拟该函数
#include <stdio.h>
#include <assert.h>
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* s1 = str1;
const char* s2 = str2;
const char* p = str1;
while (*p)
{
s1 = p;
s2 = str2;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return p;
}
p++;
}
return NULL;
}
int main()
{
char str1[] = "hello_ccccs";
char str2[] = "cccsc";
if (my_strstr(str1, str2))
printf("子串存在");
else
printf("子串不存在");
return 0;
}
5.strtok
char *strtok(char s[], const char *delim);
作用:拆分字符串,第一个形参是待拆分字符串,第二个形参分割的标志
例如this is not the test 我想把它每个单词都拆分出来,我们可以发现,每个单词都是空格隔开的,因此第二个实参就是空格
要点:
①分割标志必须要出现在待拆分字符串中,若分割标志是待拆分字符串所没有的,结果不会有改变。
②第一次使用该函数时,第一个形参要写明待拆分字符串的内容,往后的使用,第一个形参填写NULL即可。
③该函数会破坏字符串的完整性。
使用
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "This is not the test";
char str2[] = " ";
char* token;
token = strtok(str1, str2);//第一次使用必须写明待拆分字符串的内容
while (token)
{
printf("%s\n", token);
token = strtok(NULL,str2);//后面的使用只要填写NULL即可
}
return 0;
}