第九章 字符串处理函数

#pragma指令的作用:用于指定计算机操作系统特定的编译功能
#pragma warning(disable:4996) 在c文件开始处协商,告诉编译器忽略4996警告,strcpy、scanf等一些不安全的函数就可以用了。

测字符串长度函数

头文件:#include<string.h>
函数:size_t strlen(const char *s)

	char* str = "hello world";
	char str1[20] = "hello world";
	printf("%d\n", sizeof(str));//4
	printf("%d\n", sizeof(str1));//20
	printf("%d\n", strlen(str));//11
	printf("%d\n", strlen(str1));//11

sizeof是一个关键字,判断占用内存的字节数。
strlen是个函数,判断的是字符串中字符的个数。

一个汉字算两个字符长度。

	char* str = "你好";
	printf("%d\n", strlen(str));//4

字符串拷贝

strcpy

头文件:#include<string.h>
函数:char* strcpy(char *dest, const char *src);
拷贝src指向的字符串到dest指向的内存中,‘\0’也会拷贝。
注意:使用此函数的时候,必须保证dest指向的内存空间足够大,否则会出现内存污染。

	char str[100] = "hello world 111";
	//只覆盖了前面的字符
	strcpy(str, "yyyyy");
	printf("%s\n", str);//yyyyy
	printf("%s\n", str+6);//world 111
	return 0;

strncpy

头文件:#include<string.h>
函数:char* strncpy(char *dest, const char *src, size_t count);
拷贝src指向的字符串的前count个字节到dest指向的内存中,不拷贝‘\0’。
**注意:**如果count大于src指向的字符串的字符个数,则在dest后面填充n-strlen(src)个’\0’

	char str[100] = "hello world 111";
	strncpy(str, "yyyyy", 2);
	printf("%s\n", str);//yyllo world 111
	strncpy(str, "yyyyy", 6);
	printf("%s\n", str);//yyyyy

字符串追加函数

strcat

头文件:#include<string.h>
函数:char* strcat(char *dest, const char *src);
strcat函数追加src字符串追加到dest字符串后面。追加的时候会追加’\0’
注意:判断dest结尾是按\0判断的。dest的内存要足够大。

	char str[100] = "hello\0 world 111";
	strcat(str, "aaaa");
	printf("%s\n", str);//helloaaaa

strncat

头文件:#include<string.h>
函数:char* strcat(char *dest, const char *src, size_t n);
追加src字符串前n个字符追加到dest字符串后面。追加的时候会追加’\0’
如果n大于src字符个数,只将src字符追加到dest后面。

字符串比较函数

strcmp

头文件:#include<string.h>
函数:int strcmp(const char *s1, const char *s2);
比较s1和s2指向字符串的大小。
比较的方法:逐个比较ASCII码,比较出大小就返回,如果所有字符都一样,返回0,'\0’最小。
返回值:s1>s2,返回1,s1<s2返回-1,s1==s2,返回0

	char str[100] = "hello world 111";
	char* str1 = "hello you";
	int res = strcmp(str, str1);
	printf("%d", res);//-1

strncmp

头文件:#include<string.h>
函数:int strcmp(const char *s1, const char *s2, size_t n);
比较两个字符串前n个字符。

	char str[100] = "hello world 111";
	char* str1 = "hello you";
	int res = strncmp(str, str1, 5);
	printf("%d", res);//0

字符查找函数

strchr

头文件:#include<string.h>
函数:char *strchr(const char *s1, int c);
在字符指针s指向的字符串中,找ASCII码为c的字符。
注意:是首次匹配。有多个,也只会返回第一次匹配到字符的地址,找不到返回NULL

	char str[100] = "hello world 111";
	char* res = strchr(str, 'l');
	printf("%d\n", res-str);//2

strrchr

头文件:#include<string.h>
函数:char *strrchr(const char *s1, int c);
和strchr的区别是,该函数是找最后一次匹配的地址。

strstr

头文件:#include<string.h>
函数:char *strstr(const char *haystack, const char *needle);
在haystack指向的字符串中查找needle指向的字符串,也是首次匹配
找到返回字符串的首地址,没到找返回NULL

	char *str = "hello world 111";
	char* res = strstr(str, "world");
	printf("%p\n", str);//00367B30
	printf("%p\n", res);//00367B36

空间设定函数

memset

函数:void* memset(void *ptr, int value, size_t num);
memset函数是将ptr指向的内存空间的num个字节全部赋值为value
返回目的内存的首地址,即ptr的值。

	char str[100] = "hello world 111";
	memset(str, 'a', 5);
	printf("%s\n", str);//aaaaa world 111

字符串转换数值

atoi\atol\atof
头文件:#include<stdlib.h>
函数:int atoi(const char *nptr);
函数:long atol(const char *nptr);
函数:double atof(const char *nptr);
将nptr指向的字符串转换称整数。无法转换为数字,则返回0。比如‘123’转换为123。

	char *str = "123";
	char *str1 = "abv";
	int res = atoi(str);
	int res1 = atoi(str1);
	printf("%d\n", res);//123
	printf("%d\n", res1);//0

字符串切割函数

strtok

头文件:#include<string.h>
函数:char *strtok(char *str, const char *delim);
字符串切割,按照delim指向的字符串中的字符,切割str指向的字符串。
其实就是在str指向的字符串中发现了delim字符串中的字符,就将其变成’\0’
调用一次只切割一次,再去切割需要将第一次参数传NULL,意思是按照上次切割的位置继续切.
无法切割返回NULL

	char str[100] = "aaa,:.bbb,:.ccc";
	char* res = strtok(str, ",.:");
	char* res1 = strtok(NULL, ",.:");
	char* res2 = strtok(NULL, ",.:");
	printf("%s\n", res);//aaa
	printf("%s\n", res1);//bbb
	printf("%s\n", res2);//ccc

格式化字符串操作函数

sprintf

int sprintf(char *buf, const char *format, …);
输出到buf指定的内存区域。返回字符数

#include<stdio.h>
#pragma warning(disable:4996)
int main()
{
	char buf[20];
	sprintf(buf, "%d %d", 11, 22);
	printf("%s", buf);//11 22
}

sscanf

int sscanf(char *buf, const char *format, …);
从buf指定的内存区域读入信息。

#include<stdio.h>
#pragma warning(disable:4996)
int main()
{
	char *str = "2023-10-07";
	int a, b, c;
	int res = sscanf(str, "%d-%d-%d ", &a,&b,&c);
	printf("%d\n", a);//2023
	printf("%d\n", b);//10
	printf("%d\n", c);//7
	printf("%d\n", res);//3
}

高级用法

1、跳过数据:%*s或者%*d
跳过一个。

	char *str = "2023-10-07";
	int a, b;
	int res = sscanf(str, "%*d-%d-%d ", &a,&b);
	printf("%d\n", a);//10
	printf("%d\n", b);//7
	printf("%d\n", res);//2

2、读取指定宽度的数据
%[width]s

	char *str = "2023-10-07";
	char resStr[20];
	int res = sscanf(str, "%4s ", resStr);
	printf("%s\n", resStr);//2023
	printf("%d\n", res);//1

3、支持集合操作
%[a-z]表示匹配a到z中任意字符。尽可能多的匹配;遇到不匹配的则停止

	char *str = "asdfasdfa123saff";
	char resStr[100];
	int res = sscanf(str, "%[a-z] ", resStr);
	printf("%s\n", resStr);//asdfasdfa
	printf("%d\n", res);//1

%[aBc] 匹配a、B、c中的任意一员。不匹配则停止

	char *str = "asdAAfasdfa123saff";
	char resStr[100];
	int res = sscanf(str, "%[Aads] ", resStr);
	printf("%s\n", resStr);//asdAA
	printf("%d\n", res);//1

%[^aBc]匹配非aBc中的任意一员。

	char *str = "asdAAfasdfa123saff";
	char resStr[100];
	int res = sscanf(str, "%[^f] ", resStr);
	printf("%s\n", resStr);//asdAA
	printf("%d\n", res);//1

%[^a-z] 表示读取除a-z以外的所有字符。

	char *str = "hhhhjhjghasdAAfasdfa123saff";
	char resStr[100];
	int res = sscanf(str, "%[^a-f] ", resStr);
	printf("%s\n", resStr);//hhhhjhjgh
	printf("%d\n", res);//1

const

1、const修饰普通变量,代表制度的意思。
const int a = 100;以后的程序中不能修改a的值。
2、cosnt修饰指针
const char *str;
意思是str指向的内存的内容不能通过str来修改。但是恶意改变str的指向。
char * const str;
意思是str是只读的变量,str指向的内存可以修改,但是不能改变str的指向。
cosnt char * const str;
指向的内存不能修改,也不能改变指向。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七号公园的忧伤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值