注意: C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的, 字符串通常放在常量字符串中或者字符数组中。 字符串常量适用于那些对它不做修改的字符串函数.
size_t strlen ( const char * str );
代码演示:
//代码1
#include<stdio.h>
int main()
{
//a b c \0 d e f \0
//char arr[] = "abc\0def";//\0前又三个字符,结果为3
char arr[] = { 'a', 'b', 'c' }; //这里求的值是随机数,
printf("%d\n", strlen(arr));
return 0;
}
//代码2
#include<stdio.h>
int main()
{
if (strlen("abc") - strlen("abcdef") > 0)
printf(">\n");
else
printf("<\n");
return 0;
}
注意:如上代码2的结果并不是<, 而是> 因为strlen函数的默认返回值为size_t,是无符号的
#include<assert.h>
size_t my_strlen(const char* str)
{
assert(str);//保证str为空值
const char* start = str;
const char* end = str;
while (*end != '\0')
{
end++;
}
return end - start;//指向\0的指针-指向初始位置的指针,得到的是\0之前元素的个数
}
int main()
{
char arr[] = "abcdef";
int len = my_strlen(arr);
printf("%d\n", len);
return 0;
}
char* strcpy(char * destination, const char * source );
示例:
int main()
{
char arr[10] = { 0 };
const char* p = "abcdef";
strcpy(arr, p);
printf("%s\n", arr);
return 0;
}
错误示例:
int main()
{
char arr[10] = "xxxxxxxxx";
//const char* p = "abcdef";//错误,源字符串中必须出现\0
char arr2[] = { 'b', 'i','\0', 't'};
//strcpy(arr, p);//错误
strcpy(arr, arr2);
printf("%s\n", arr);
return 0;
}
int main()
{
char arr[3] = {0};//错误,目标空间必须足够大
char arr2[] = "abcdef";
strcpy(arr, arr2);
printf("%s\n", arr);
return 0;
}
int main()
{
char* p = "hello world";//错误,目标空间不能是常量字符串
char arr2[] = "abcdef";
strcpy(p, arr2);
printf("%s\n", p);
return 0;
}
//strcpy模拟实现
#include<assert.h>
char* my_strcpy(char*dest,const char*src)
{
assert(dest);
assert(src);
char* ret = dest;
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "abc";
char arr2[] = "hellow world";
printf("%s\n", my_strcpy(arr1, arr2));
return 0;
}
阅读资料,我们可以发现strcat函数和strcpy函数的用法类似
示例:
int main()
{
char arr1[20] = "hellow";
char arr2[] = "world";
stract(arr1, arr2);//字符追加函数
printf("%s\n,arr1");
return 0;
}
输出结果为:hello world
模拟实现:
//strcat模拟实现
char* my_strcat(char* dest, const char* src)
{
//1.找目标空间中的\0
char* cur = dest;
while (*cur != '\0')
{
cur++;
}
//2、拷贝源头数据到\0之后的空间
while (*cur++ = *src++)
{
;
}
return dest;
}
int main()
{
char arr1[20] = "hello \0 xxxxxxxxx";
char arr2[] = "world";
my_strcat(arr1, arr2);//字符追加函数
printf("%s\n",arr1);
return 0;
}
int strcmp ( const char * str1, const char * str2 );
//strcmp字符比较函数,比较的是每个字符的大小
int main()
{
//char arr1[] = "abcdef";
//char arr2[] = "abq";
//char arr1[] = "abcd";
//char arr2[] = "abc";
char arr1[] = "abc";
char arr2[] = "abc";
//char arr1[] = { 'a', 'b', 'c' };//需要有\0
//char arr2[] = { 'a', 'b', 'c' };
int ret = strcmp(arr1, arr2);
if (ret < 0)
printf("arr1<arr2\n");
else if(ret>0)
printf("arr1>arr2\n");
else
printf("arr1==arr2\n");
printf("%d\n", ret);
return 0;
}
模拟实现strcmp:
//int my_strcmp(const char*s1, const char*s2)
//{
// assert(s1 && s2);//使s1,s2不为空
// while (*s1 == *s2)
// {
// if (*s1 == '\0')
// {
// return 0;
// }
// s1++;
// s2++;
// }
// if (*s1 > *s2)
// return 1;
// else
// return -1;
//}
//
int my_strcmp(const char* s1, const char* s2)//比较而不希望改变加const
{
assert(s1 && s2);
while (*s1 == *s2)
{
if (*s1 == '\0')
{
return 0;
}
s1++;
s2++;
}
return *s1 - *s2;
}
int main()
{
//char arr1[] = "abcdef";
//char arr2[] = "abq";
//char arr1[] = "abcd";
//char arr2[] = "abc";
char arr1[] = "abc";
char arr2[] = "abc";
//char arr1[] = { 'a', 'b', 'c' };//需要有\0
//char arr2[] = { 'a', 'b', 'c' };
int ret = my_strcmp(arr1, arr2);
if (ret < 0)
printf("arr1<arr2\n");
else if (ret > 0)
printf("arr1>arr2\n");
else
printf("arr1==arr2\n");
printf("%d\n", ret);
return 0;
}
下面的函数和以上函数相似,从名称可以看出其内部多加了n 即我们可以对参数有所限制
char * strncpy ( char * destination, const char * source, size_t num );
示例:
#include<string.h>
int main()
{
char arr1[20] = "abcdefghi";
char arr2[] = "xxxx";
strncpy(arr1, arr2, 2);
printf("%s\n", arr1);
return 0;
//运行结果为xxxxefghi
}
#include<string.h>
int main()
{
char arr1[20] = "abcdefghi";//目标空间
char arr2[] = "xxxx";//源字符串
strncpy(arr1, arr2, 8);//num>源字符串长度
printf("%s\n", arr1);
return 0;
//运行结果为xxxx 在目标的后边追加0,直到num个。
}
char * strncat ( char * destination, const char * source, size_t num );
int main()
{
char arr1[20] = "abcdef\0qqqqqq";
char arr2[] = "xyz";
strncat(arr1, arr2, 2);
printf("%s\n", arr1);
return 0;//abcdefxy
//从\0开始追加,追加结束后会自动补\0
}
int main()
{
char arr1[20] = "abc";
strncat(arr1, arr1, 3);
printf("%s\n", arr1);
return 0;
//与strcat不同的是strncat可以给自己追加
}
int main()
{
int ret = strncmp("abcdef", "abc", 3);
printf("%d\n", ret);
return 0;//0
}
int main()
{
int ret = strncmp("abcdef", "abc", 4);
printf("%d\n", ret);
return 0;//1
}
这里我们关于C】字符函数和字符串函数(上)的内容就介绍完了,文章中某些内容我们之前有介绍,所以只是一笔带过,还请谅解。希望以上内容对大家有所帮助👀,如有不足望指出🙏
加油!!