头文件:#include<stdio.h>
共同特点:遇"\0"结束
1.strlen 字符串长度函数
原型 :
int strlen ( const char *str )
功能 : 计算字符串长度,不含’\0'
参数 : 存放字符串的内存空间首地址
说明 : 形式参数str用const修饰,表示str指向的空间中的数据只读不可修改
返回值:字符串长度(不含 ’\0' )
void text01()
{
//strlen
char str1[128] = "hello world";
char str2[] = "hello w\0orld";
// \XXX : 八进制转义 : 0-7
char str3[] = "hello w\12orld";
// 这里的\x3f为十六进制转义 : 0-9 ; a-f
char str4[] = "hello w\x3forld";
printf("%d\n", sizeof(str1)); //128
printf("%d\n", strlen(str1)); //11
printf("%d\n", sizeof(str2)); //13
printf("%d\n", strlen(str2)); //7
printf("%d\n", sizeof(str3)); //13
printf("%d\n", strlen(str3)); //12
printf("%d\n", sizeof(str4)); //13
printf("%d\n", strlen(str4)); //12
}
2.strcpy / strncpy 字符串拷贝
strcpy
原型:
char *strcpy( char *dest,const char *src);
功能:把 src 所指向的字符串复制到 dest 所指向的空间中
返回值:返回dest字符串的首地址
注意 : ’\0’也会拷贝过去
void text02()
{
char str1[16] = ""; //保证足够长
char str2[] = "world";
char str3[16] = "hello ";
char str4[] = "wor\0ld";
strcpy(str1, str2);
printf("%s\n", str1); // world
strcpy(str3, str4);
printf("%s\n", str3); // wor
}
strncpy
原型:
char *strncpy( char *dest, const char *src, int num);
功能:把 src 指向字符串的前 num 个复制到 dest 所指向的空间中
返回值:返回 dest 字符串的首地址
注意:’\0’不拷贝
void text03()
{
char buf[32] = "";
printf("%s", strncpy(buf, "hello world", 3)) //hel
return 0;
}
3. strcat / strncat 字符串拼接
strcat
原型:
char *strcat(char *str1, char *str2);
功能:将 str2 连接到 str1 后面
返回值:返回 str1 字符串的首地址
注意:’\0’会一起拷贝过去
void text04()
{
char str1[16] = "hello ";
char str2[16] = "world";
char str3[16] = "hello ";
char str4[16] = "wor\0ld";
char str5[16] = "hel\0lo ";
char str6[16] = "world";
strcat(str1, str2);
printf("%s\n", str1); // hello world
strcat(str3, str4);
printf("%s\n", str3); // hello wor
strcat(str5, str6);
printf("%s\n", str5); // helworld
}
strncat
原型:
char *strcat(char *str1, char *str2, int n);
功能 : 将 str2 的前 n 个连接到 str1 后面
返回值:返回 str1 字符串的首地址
注意: ’\0’ 会一起拷贝过去
void test05()
{
char src[ ]="world" ;char dst[ 128]="hello" ;
strncat ( dst, src ,2);
printf( "%s \n" , dst); // "hellowo "
}
4. strcmp / strncmp 字符串比较
strcmp
原型:
int strcmp( char *str1, char *str2);
功能:比较str1和str2的大小;
返回值:
- 相等 返回0;
- str1大于str2 返回>0
- str1小于str2 返回<0
void text06(void)
{
char str1[16] = "Hello World";
char str2[16] = "Hello world ";
if (strcmp(str1, str2) == 0)
printf("str1 == str2\n");
else if (strcmp(str1, str2) > 0)
printf("str1 > str2\n");
else
printf("str1 < str2\n");
//输出结果:str1 < str2
}
strncmp
strncmp的用法与strcmp用法基本一致;多了一个参数n;只比较前n个元素
void text07(void)
{
char str1[16] = "Hello World";
char str2[16] = "Hello world ";
if (strncmp(str1, str2, 5) == 0)
printf("str1 == str2\n");
else if (strncmp(str1, str2, 5) > 0)
printf("str1 > str2\n");
else
printf("str1 < str2\n");
//输出结果:str1 == str2
}
5. strchr 字符查找函数
原型 :
char* strchr( const char *str1,char ch);
功能 : 在字符串str1中查找字母ch第一次出现的位置
返回值 :
- 返回第一次出现的ch地址
- 如果找不到,返回NULL
例:把字符串中的 'o' 替换成 '#'
void text08(void)
{
char s1[] = "hello world";
char *p1 = NULL;
while (1)
{
p1 = strchr(s1, 'o');
if (p1 == NULL)
break;
*p1 = '#';
}
printf("%s\n", s1); // hell# w#rld
}
6. strstr 字符串查找函数
原型 :
char* strchr( const char *str1,char* str2);
功能:在字符串str1中查找字符串str2第一次出现的位置
返回值:
- 返回第一次出现的str2地址
- 如果找不到,返回NULL
void text09(void)
{
char s1[] = "hello world";
printf("%s\n", strstr(s1, "lo")); //lo world
}
7. atoi/ato1/atof //字符串转换功能
头文件:#include<stdlib>
原型:
int atoi (const char*str);
long atol (const char*str);
double atof (const char*str);
功能:将str所指向的数字字符串转化为 int / long / double
void test10()
{
printf("%d\n", atoi( "123")); //123
printf("%ld\n", atol("12345"));//12345
printf("%f\n ", atof("12.3")); //12.3
}
8.strtok字符串切割函数
原型:
char *strtok (char s[], const char *delim);
功能: strtok()用来将字符串分割成一个个片段。
参数:
- s指向欲分割的字符串
- delim则为分割字符串中包含的所有字符。
返回值:
- 切割成功返回切割到字符换片段的首元素地址―
- 失败:NULL
当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0字符,当连续出现多个时只替换第一个为\0。
在第一次调用时:strtok()必需给予参数s字符串往后的调用则将参数s设置成NULL,每次调用成功则返回指向被分割出片段的指针
void text11()
{
char buf[] = "apple;banana;pear;peach";
char *arr[16] = {buf};
int i = 0;
while (1)
{
arr[i] = strtok(arr[i], ";");
if (arr[i++] == NULL)break;
}
for (i = 0; arr[i] != NULL; i++){
printf("%s\n", arr[i]);
}
}
9.sprintf组包函数
标准I/0库函数
int sprintf(buf,"格式",数据)
// buf:用来存放组好的报文
//"格式":按照格式组包//数据:各个零散的数据
返回值 : 返回值的是组好的报文的实际长度(不包含'\0')
void test12()
{
int year = 2021;
int mon = 5;
int day = 29;
int len = 0;
char s[128] = "";
len = sprintf(s, "%d年%d月%d日", year, mon, day);
printf("len = %d \n", len); //len = 13
printf("buf=%s \n", s); //buf=2021年5月29日
}
10.sscanf拆包
标准I/0库函数
跳过数据:%*s或%*d
sscanf("1234 5678",“%*d %s",buf);
读指定宽度的数据:%[width]s
sscanf("12345678","%4s",buf);
支持集合操作:
- %[a-z] 表示匹配a到z中任意字符(尽可能多的匹配)
- %[aBc] 匹配a、B、c中一员,贪婪性
- %[^a] 匹配非a的任意字符,贪婪性
- %[^a-z] 表示读取除a-z以外的所有字符
void test13()
{
char buf[]="2021年5月29日";
int year=0;
int mon=0;
int day=0;
//%d只能提取'O'~'9'
sscanf(buf, "%d年%d月%d日", &year, &mon,&day );
printf("year = %d \n", year);//2021
printf("mon = %d \n", mon); //5
printf("day = %d\n", day); //29
}
11.const关键字
1. const修饰变量,作用是将变量常量化,使变量‘只读’
void test14()
{
// const修饰num为只读变量num只能取值num不能被赋值
const int num = 10;
printf ( "num = %d\n ", num); // num = 100;
//err num不能被赋值
printf ( "&num = %p\n", &num);
//但是如果知道num的地址 也可以间接的修改num的值
(int *) (&num) = 1000;
printf ( "num = %d \n" , num);
}
2. const int *p;
用户不能借助*p更改空间的内容但是p可以指向其他空间(*p只读 p可读可写)
void test15()
{
int num1 = 10;int num2 = 20;
// const在*的左边*p只读p可读可写const int *p = &numl ;
printf("*p =%d \n",*p);// 10//*p = 1000; //err *p只读
p=&num2; //p可读可写
printf("*p = %d \n",*p);//20
}
3.int * const p;
用户可以通过*p修改p所指向空间的内容但是不能再更改p的指向(*p可读可写p只读)
void text16()
{
int a = 10;
int b = 20;
int * const p = &a;
// p = &b;
// error C2166 : 左值指定 const 对象
// 但可对 *p 可读可写
*p = 100;
printf("%d\n", *p); //100
}
4、const int * const p;
(*p只读p只读)
void text17()
{
int a = 10;
int b = 20;
const int *const p = &a;
//p = &b;
//error C2166: 左值指定 const 对象
//只能对p进行读操作
printf("%d\n", *p);
}