大家好我是沐曦希
C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在 常量字符串 中或者 字符数组 中。 字符串常量适用于那些对它不做修改的字符串函数
size_t strlen ( const char * str );
//typedef unsigned int size_t;
1.字符串中’\0’ 作为结束标志,strlen函数返回的是在字符串中 ‘\0’ 前面出现的字符个数(不包含 ‘\0’ )。
2.参数指向的字符串必须要以 ‘\0’ 结束。
3.注意函数的返回值为size_t,是无符号的。
#include
#include
int main()
{
char arr[] = "abcdef";
size_t len = strlen(arr);
printf("%u\n", len);//6
return 0;
}
注:
#include
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函数的返回值是无符号整型,无符号整型之间运算的结果还是无符号整型,即恒大于零。
故if (strlen(str2) - strlen(str1) > 0)应该改为if(strlen(str2)>strlen(str1))。
#include
#include
//模拟实现strlen函数
size_t my_strlen(const char* str)
{
assert(str);
size_t count = 0;
while (*str++ != '\0')
count++;
return count;
}
int main()
{
char arr[] = "abcdef";
size_t n = my_strlen(arr);
printf("%u\n", n);
return 0;
}
#include
#include
size_t my_strlen(const char* str)
{
assert(str);
char* end = str;
while (*end++ != '\0');
size_t n = (size_t)(end - str) - 1;
return n;
}
int main()
{
char arr[] = "abcde";
size_t n = my_strlen(arr);
printf("%u\n", n);
return 0;
}
#include
#include
size_t my_strlen(const char* str)
{
if (*str == '\0')
return 0;
else
return 1 + my_strlen(str + 1);
}
int main()
{
char arr[] = "abcdef";
size_t n = my_strlen(arr);
printf("%u\n", n);
return 0;
}
char* strcpy(char * destination, const char * source );
1.Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).(将源指向的 C 字符串复制到目标所指向的数组中,包括终止空字符(并在该点停止)。)
2.源字符串必须以 ‘\0’ 结束。
3.会将源字符串中的 ‘\0’ 拷贝到目标空间。
4.目标空间必须足够大,以确保能存放源字符串。
5.目标空间必须可变。(即不能是指针)
#include
#include
int main()
{
char arr1[] = "abcd";
char arr2[20] = "xxxxxxxxx";
//arr2="abcd";//error//arr2数组是地址,地址是一个常量值,不能被赋值
strcpy(arr2, arr1);
printf("%s\n", arr2);
return 0;
}
#include
#include
char* my_strcpy(char* dest, const char* src)
{
assert(dest && src);
char* str = dest;
while (*dest++ = *src++);
return str;
}
int main()
{
char arr1[] = "abcdef";
char arr2[20] = { '\0' };
my_strcpy(arr2, arr1);
printf("%s\n", arr2);
return 0;
}
char * strcat ( char * destination, const char * source );
1.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.(将源字符串的副本追加到目标字符串。目标中的终止空字符被源的第一个字符覆盖,并且由目标中两者的串联形成的新字符串的末尾包含一个空字符。)
2.源字符串必须以 ‘\0’ 结束。
3.目标空间必须有足够的大,能容纳下源字符串的内容。
4.目标空间必须可修改。(即不能是指针)
5.字符串自己不能给自己追加,否则回陷入死循环。
#include
#include
int main()
{
char arr[20] = "abcd";
strcat(arr, arr);
printf("%s\n", arr);
return 0;
}
#include
#include
int main()
{
char arr[20] = "hello ";
strcat(arr, "world");
printf("%s\n", arr);
return 0;
}
#include
#include
char* my_strcat(char* dest, const char* src)
{
assert(dest && src);
char* str = dest;
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++);
return str;
}
int main()
{
char arr[20] = "hello ";
my_strcat(arr, "world");
printf("%s\n", arr);
return 0;
}
int strcmp ( const char * str1, const char * str2 );
1.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.(此函数开始比较每个字符串的第一个字符。如果它们彼此相等,则继续使用以下对,直到字符不同或达到终止空字符。)
2.标准规定:
- 第一个字符串大于第二个字符串,则返回大于0的数字
2.第一个字符串等于第二个字符串,则返回0
3.第一个字符串小于第二个字符串,则返回小于0的数字
strcmp比较的是两字符的ASCI码值
#include
#include
int main()
{
char arr1[] = "abcdef";
char arr2[] = "azcd";
if (strcmp(arr1, arr2) > 0)
printf("arr1>arr2\n");
else
printf("arr1);
return 0;
}
#include
#include
//int strcmp( const char *string1, const char *string2 );
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
return (*str1 - *str2);
}
int main()
{
char arr1[20] = { "zhangsan" };
char arr2[] = { "zhangsanfeng" };
//比较两字符串是否相等
int ret = my_strcmp(arr1, arr2);
printf("%d\n", ret);
return;
}
那么这一次的字符函数和字符串函数的笔记就到这里啦。小沐会持续更新字符函数和字符串函数。
加油!既然选择了远方,便只顾风雨兼程,要一直前进,直到遇见那些我曾热爱的终点,才能没有遗憾,才会特别幸福!