目录
0. 前言
C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在 常量字符串 中或者 字符数组 中。
字符串常量 适用于那些对它不做修改的字符串函数.
1. 函数介绍
1.1 strlen字符串长度函数的使用及模拟实现
size_t strlen ( const char * str );
- 字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包 含 '\0' )。
- 参数指向的字符串必须要以 '\0' 结束。
- 注意函数的返回值为size_t,是无符号的( 易错 )
- 学会strlen函数的模拟实现
//1.strlen _ 头文件string.h__求字符串长度
//1.1 strlen函数的使用
#include<string.h>
int main()
{
//char ch = 'a';
//"abcdef";//C语言没有字符串类型
//分为两种情况,一种有'\0',一种没有'\0'
char arr[] = "abcdef";
int len = strlen(arr);//string length
printf("%d\n", len);//6
char arr1[] = { 'a','b','c','d','e','f'};
int len1 = strlen(arr1);
printf("%d\n", len1);//随机值
char arr2[10] = { 'a','b','c','d','e','f' };
//未完全初始化,默认后续元素为0,0 <==>'\0'
int len2 = strlen(arr1);
printf("%d\n", len2);//6
//if ((int)strlen("abc") - (int)strlen("qwerty") > 0)
if (strlen("abc") - strlen("qwerty") > 0)//strlen函数返回值为size_t无符号数
//这里会进行整型提升,无符号 - 无符号 还是无符号
{
printf(">\n");
}
else
printf("<\n");
return 0;
}
//1.2 模拟实现strlen
#include<assert.h>
size_t my_strlen(const char* str)
{
assert(str);//害怕str为空指针,所以就断言判断
size_t count = 0;
while (*str)
{
str++;
count++;
}
return count;
}
int main()
{
char arr[] = { "abcdef" };
int len = my_strlen(arr);
printf("%d\n", len);
return 0;
}
三种方法实现strlen函数:1.计数器方法,2.递归方法,3.指针减指针方法
1.2 strcpy字符串拷贝函数的使用及模拟实现
char* strcpy(char * destination, const char * source );
- Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
- 源字符串必须以 '\0' 结束。
- 会将源字符串中的 '\0' 拷贝到目标空间。
- 目标空间必须足够大,以确保能存放源字符串。
- 目标空间必须可变。
- 学会模拟实现。
//2.strcpy_头文件string.h___拷贝字符串
//2.1 strcpy的使用
#include<string.h>
int main()
{
char arr1[20] = { 0 };
char arr2[] = { "abcdef" };
strcpy(arr1, arr2);
printf("%s\n", arr1);
//注意1.
//char arr2[] = { 'a','b','c' };//err_程序 运行时错误 崩溃
//strcpy(arr1, arr2);
//源头字符串必须要有'\0'
//注意2.
char arr3[20] = { "xxxxxxxxxxxxxxx" };
//字符串拷贝也会将源字符串的'\0'拷贝进去
strcpy(arr3, arr2);
printf("%s\n", arr3);//abcdef
//注意3.
//char arr4[5] = { 0 }; //err ——————程序崩溃,arr4栈区空间被破坏了
//源字符串的大小必须小于目标字符串的大小
//strcpy(arr4, arr2);
//注意4:
//char* ar1[] = { "qwertyuiop"};//err__ar1指向的是常量字符串,常量是不可修改的,程序崩溃
//char ar2[] = { "abcdef" };
目标空间必须可变
//strcpy(ar1, ar2);
return 0;
}
//2.2 strcpy 模拟实现
#include<assert.h>
//strcpy返回的目标空间的起始地址
//strcpy函数的返回类型的设置是为了实现链式访问
char* my_strcpy(char* destination, const char* source)
{
char* dest = destination;
assert(destination && source);
while (*destination++ = *source++)
{
;
}
return dest;
}
int main()
{
char arr[20] = { "ijklouijjij"};
char arr1[] = { "abcdef" };
//my_strcpy(arr, arr1);
//printf("%s", arr);
printf("%s", my_strcpy(arr, arr1));//根据返回类型,可以直接打印,实现链式访问
return 0;
}
strcpy函数的返回类型的设置是为了实现链式访问
函数的链式访问就是把一个函数的返回值作为另外一个函数的参数。
1.3 strcat字符串追加函数的使用及模拟实现
char * strcat ( char * destination, const char * source );
- 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.
- 源字符串必须以 '\0' 结束。
- 目标空间必须有足够的大,能容纳下源字符串的内容。
- 目标空间必须可修改。
- 字符串自己给自己追加,如何?
//3.strcat函数__字符串追加函数
//3.1 strcat函数的使用
#include<string.h>
int main()
{
char arr1[20] = { "Hello\0xxxxxxxxx" };
char arr2[] = { "Computer!" };
printf("%s\n", strcat(arr1, arr2));//HelloComputer!
//可观察出strcat同时将原目标字符串'\0'也追加了进去;
//同时知其返回类型也是指向数组arr1的字符指针
char arr11[20] = { "Hello\0xxxxxxxxx" };
char arr3[] = { 'a','b','c' };
printf("%s\n", strcat(arr11, arr3));//err——程序无运行结果
//由此可见,strcat在使用的时候源字符串必须要有'\0'
/*目标空间必须有足够的大,能容纳下源字符串的内容。
目标空间必须可修改。*/
return 0;
}
//3.2 strcat函数的模拟实现
#include<assert.h>
char* my_strcat(char* destination,const char* source)
{
assert(destination && source);
char* dest = destination;
while (*destination)
{
destination++;
}
while (*destination++ = *source++)
{
;
}
return dest;
}
int main()
{
char arr1[20] = { "Hello" };
char arr2[] = { "Boys!!!" };
printf("%s", my_strcat(arr1, arr2));
}
//在自己模拟函数中
自己给自己追加字符串————会发现自己源字符串的'\0'一直正在被自己覆盖,死循环
但是库里的strcat函数可以实现自己给自己追加字符串
1.4 strcmp字符串比较函数的使用及模拟实现
int strcmp ( const char * str1, const char * str2 );
- 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.
- 标准规定: 第一个字符串大于第二个字符串,则返回大于0的数字
- 第一个字符串等于第二个字符串,则返回0
- 第一个字符串小于第二个字符串,则返回小于0的数字
- 那么如何判断两个字符串?
//strcmp函数在比较的时候,比较不是字符串的长度,而是比较字符串中对定位置上的字符的大小,如果相同,就比较下一对,直到不同或者遇到'\0',比较的是ascii码值
//4.strcmp字符串比较函数的使用__头文件string.h
#include<string.h>
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abq";
int ret = strcmp(arr1, arr2);
printf("%d\n", ret);
//strcmp函数在比较的时候,比较不是字符串的长度,而是比较字符串中对定位置上的字符的大小
//如果相同,就比较下一对,直到不同或者遇到'\0',比较的是ascii码值
//返回值类型是int,vs编译器下实现strcmp返回值是0,1,-1
//但不代表所有编译器都是这么实现的
/*标准规定: 第一个字符串大于第二个字符串,则返回大于0的数字
第一个字符串等于第二个字符串,则返回0
第一个字符串小于第二个字符串,则返回小于0的数字*/
return 0;
}
//4.strcmp字符串比较函数的模拟实现
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
{
return 0;
}
str1++;
str2++;
}
/*if (*str1 > *str2)
{
return 1;
}
else
return -1;*/
return *str1 - *str2;
}
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abc";
int ret = my_strcmp(arr1, arr2);
if (ret > 0)
{
printf(">\n");
}
else if (ret == 0)
{
printf("==\n");
}
else
{
printf("<\n");
}
return 0;
}
注意:
strcpy
strcmp
strcat
这些函数都是长度不受限制的字符串函数,在使用的时候就会有一定的风险
所以,就有了
strncpy
strncat
strncmp
这三个长度受到限制的字符串函数
1.5 strncpy函数的使用
char * strncpy ( char * destination, const char * source, size_t num );
- Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
- 拷贝num个字符从源字符串到目标空间。
- 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
//strncpy函数的使用
#include<string.h>
int main()
{
char arr1[] = "abcdef";
char arr2[] = "qwertyuiop";
strncpy(arr1, arr2,3);
//增加一个参数,指定拷贝元素个数
//相对安全
printf("%s\n", arr1);
return 0;
}
1.6 strncat函数的使用
char * strncat ( char * destination, const char * source, size_t num );
- Appends the first num characters of source to destination, plus a terminating null-character.
- If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.
//strncat函数的使用
#include<string.h>
int main()
{
char arr1[20] = "abcdef\0xxxxxxxxxx";
char arr2[] = "qwer";
//strncat(arr1, arr2, 7);
//增加一个参数,指定追加字符个数
//strncat追加从'\0'追加,并追加完会放入'\0'
//这里并会追加7个,而是只把有限的字符追加进去,并加入'\0'
strncat(arr1, arr1,5);
printf("%s\n", arr1);
return 0;
}
1.7 strncmp函数的使用
int strncmp ( const char * str1, const char * str2, size_t num );
比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。
#include <stdio.h>
#include <string.h>
int main()
{
char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
int n;
puts("Looking for R2 astromech droids...");
for (n = 0; n < 3; n++)
if (strncmp(str[n], "R2xx", 2) == 0)
{
printf("found %s\n", str[n]);
}
return 0;
}