C 语法 - 09_字符串处理函数

// string.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <stdio.h>

//strlen 测量字符串长度,返回字符串的实际长度
void test01()
{
	char str[128] = "hello";
	printf("%d\n", strlen(str));

}

//strcpy 字符串拷贝函数, 返回拷贝后的新的字符串首地址
void test02()
{
	char dst[128] = "";
	char src[] = "hello\0world";
	strcpy(dst, src);
	printf("%s\n", dst);

	char dst1[] = "";
	char src1[] = "helloworld";
	//char* ss = strcpy(dst1, src1);     //error  拷贝越界
	//printf("%s\n", dst1);

	char dst2[128] = "";
	char src2[] = "hello world";
	strncpy(dst2, src2, 3);
	printf("%s\n", dst2);

}

//strcat 字符串追加函数, 返回拷贝后的新的字符串首地址
void test03()
{
	char dst[128] = "hello";
	char src[] = "world";
	strcat(dst, src);
	printf("%s\n", dst);

	char dst1[128] = "hello";
	char src1[] = "world";
	strncat(dst1, src1, 2);
	printf("%s\n", dst1);

}

//strcmp 字符串比较函数
void test04()
{
	char s1[128] = "";
	char s2[128] = "";

	printf("请输入第一个字符串:");
	scanf("%s", s1);

	printf("请输入第二个字符串:");
	scanf("%s", s2);

	//if (strcmp(s1, s2) == 0) printf("%s 等于 %s\n", s1, s2);
	//else if (strcmp(s1, s2) > 0) printf("%s 大于 %s\n", s1, s2);
	//else if (strcmp(s1, s2) < 0) printf("%s 小于 %s\n", s1, s2);

	printf("请输入需要比较的字符个数:");
	int n = 0;
	scanf("%d", &n);
	if (strncmp(s1, s2, n) == 0) printf("两字符串的前%d个字符, %s 等于 %s\n", n, s1, s2);
	else if (strncmp(s1, s2, n) > 0) printf("两字符串的前%d个字符, %s 大于 %s\n", n, s1, s2);
	else if (strncmp(s1, s2, n) < 0) printf("两字符串的前%d个字符, %s 小于 %s\n", n, s1, s2);

}

//strchr 字符查找函数,成功返回查找到的字符地址,失败返回NULL。
void test05()
{
	char str[] = "hello world";

	char* ret = strchr(str, 'o');
	if (ret != NULL) printf("%s\n", ret);

	char* ret1 = strrchr(str, 'o');
	if (ret != NULL) printf("%s\n", ret1);

}

//strstr 字符串查找函数,成功返回查找到的字符串首元素地址,失败返回NULL。
void test06()
{
	char str[] = "https://blog.csdn.net/tan.888/tan.666/tan.yw/tan.688";
	printf("%s\n", str);

	char* ret1 = str;
	while (1)
	{
		ret1 = strstr(ret1, "tan");
		if (ret1 == NULL) break;

		//memset(ret1, '*', strlen("tan"));

		char strCp[] = "@$&%";
		strncpy(ret1, strCp, 3);
	}
	printf("%s\n", str);

}

//atoi atol atoll atof 字符串转数值
void test07()
{
	printf("%d\n", atoi("100ab"));
	printf("%ld\n", atol("100ab"));
	printf("%ld\n", atoll("100ab"));
	printf("%f\n", atof("100ab"));

}

//strtok 字符串切割函数,成功返回切割出来的字符串首元素地址,失败返回NULL。第一个形参,第一次切割传字符串首元素地址,后续切割传NULL。
void test08()
{
	char str[] = "aa,ff,ss,gg,ee,hh,rr";
	char* buf[30] = { str };

	int i = 0;

	while ((buf[i] = strtok(buf[i], ",")) && ++i);
	//while (1)
	//{
	//	buf[i] = strtok(buf[i], ",");
	//	if (buf[i] == NULL) break;

	//	i++;
	//}

	i = 0;
	while (buf[i] != NULL)
	{
		printf("%s\n", buf[i]);
		i++;
	}

}

//手机短信解析案例
int msg_deal(char* msg_src, char* msg_done[], char* str)
{
	//int i = 0;
	//msg_done[i] = strtok(msg_src, str);
	//while (msg_done[i] != NULL)
	//{
	//	i++;
	//	msg_done[i] = strtok(NULL, str);
	//}

	int i = 0;
	msg_done[i] = strtok(msg_src, str);
	if (msg_done[i] == NULL) return i;

	while (++i && (msg_done[i] = strtok(NULL, str)));

	return i;

}
void test09()
{
	char msg_src[] = "+CMGR:REC UNREAD,+8618806640042,23/01/06,17:40:00+00,ABCDFEG";

	char* msg_done[30] = { NULL };
	int num = 0;
	num = msg_deal(msg_src, msg_done, (char*)",");

	printf("字符串的数量:%d\n", num);

	//for (int i = 0; i < num; i++)
	//{
	//	printf("%s\n", msg_done[i]);
	//}

	int i = 0;
	while (msg_done[i] != NULL)
	{
		printf("%s\n", msg_done[i++]);
	}

}

//atoi封装函数
int my_atoi(char* buf)
{
	int my_int = 0;

	//int i = 0;
	//while (buf[i] >= '0' && buf[i] <= '9')
	//{
	//	my_int = my_int * 10 + (buf[i++] - '0');
	//}

	while (*buf >= '0' && *buf <= '9')
	{
		my_int = my_int * 10 + (*(buf++) - '0');
	}

	return my_int;

}
void test10()
{
	char str[] = "123a45abc";

	printf("%d\n", my_atoi(str));

}

//sprintf 组包函数,将零散数据按固定格式,组成字符串。返回实际组包的长度。
void test11()
{
	int year = 2023;
	int month = 1;
	int day = 8;

	char buf[128] = "";
	int len = sprintf(buf, "%d年%d月%d日\n", year, month, day);

	printf("%d %s", len, buf);

}

//sscanf 解包函数,将一串字符串按固定的格式提取出数据信息。
void test12()
{
	char buf[128] = "2023年 01月08日";
	int year = 0;
	int month = 0;
	int day = 0;

	sscanf(buf, "%d年%d月%d日", &year, &month, &day);   //%d提取字符 '0' ~ '9'
	printf("%d %d %d\n", year, month, day);

	char msg[128] = "";
	sscanf(buf, "%s", msg);            //%s遇到 '\0' 空格 回车 结束
	printf("%s\n", msg);

}

//跳过数据 %*d %*s
void test13()
{
	char buf[128] = "";
	sscanf("123:::456", "%*d:%s", buf);
	printf("%s\n", buf);

}

//读取指定宽度数据 %3d %3s
void test14()
{
	int num = 0;
	char buf[128] = "";

	sscanf("12345678", "%*2d %2d %*c %2s", &num, buf);
	printf("%d %s\n", num, buf);

}

//%[a-z] %[abc] %[a, c] %[^ab] 尽可能多的匹配区域字符
void test15()
{
	char buf[128] = "";
	char buf1[128] = "";
	sscanf("abcABCfg123", "%[a-d, A-B, C, fg]", buf);
	sscanf("abcABCfg123", "%[^ABc]", buf1);            //%[^ab]非,遇到结束匹配

	printf("%s\n", buf);
	printf("%s\n", buf1);

}

//综合案例1
void test16()
{
	char buf[128] = "[02:04.96][00:36.08]你好啊!";

	char* str1 = buf;
	//while ((strncmp(str1, "[", 1) == 0) && (str1 += 10));
	while ((*str1 == '[') && (str1 += 10));

	char* time = buf;
	while (*time == '[')
	{
		int fen = 0, miao = 0;

		sscanf(time, "[%d:%d", &fen, &miao);
		printf("时间%d秒说:%s\n", fen * 60 + miao, str1);

		time += 10;
	}

}

//综合案例2
void test17()
{
	char buf[128] = "tanyouwen@qq.com";
	char name[128] = "";
	char log[128] = "";

	sscanf(buf, "%[^@]@%[^.]", name, log);
	printf("name = %s, log = %s\n", name, log);

}

//综合案例3
void test18()
{
	char buf[] = "+CMGR:REC UNREAD,+8618806640042,23/01/06,17:40:06+00,ABCDFEG";
	char* msg[32] = { buf };

	int i = 0;
	while ((msg[i] = strtok(msg[i], ",")) && (++i));

	//msg[0] = "+CMGR:REC UNREAD";
	char status[128] = "";
	sscanf(msg[0], "%*s %s", status);
	printf("短信读取状态为:%s\n", status);

	//msg[1] = "+8618806640042";
	//char num[128] = "";
	//sscanf(msg[1] + 3, "%s", num);
	//printf("手机号码为:%s\n", num);
	printf("手机号码为:%s\n", msg[1] + 3);

	//msg[2] = "23/01/06";
	int year = 0, month = 0, day = 0;
	sscanf(msg[2], "%d/%d/%d", &year, &month, &day);
	printf("日期:%d年%02d月%02d日\n", year + 2000, month, day);

	//msg[3] = "17:40:06+00";
	int shi = 0, fen = 0, miao = 0;
	sscanf(msg[3], "%d:%d:%d", &shi, &fen, &miao);
	printf("时间为:%02d时%02d分%02d秒\n", shi, fen, miao);

	//msg[4] = "ABCDFEG";
	printf("短信内容为:%s\n", msg[4]);

}

//const 修饰为只读
void test19()
{
	const int num = 10;         //const 修饰普通变量,为只读
	//num = 20;   //error

	const int* p = NULL;       //const 修饰 *,*p 为只读
	//*p = 100;     //error
	p = &num;
	printf("*p = %d\n", *p);

	int mum1 = 20;
	int* const p1 = &mum1;      //const 修饰 指针变量,p1 为只读
	//p1 = NULL;    //error
	*p1 = 30;
	printf("*p1 = %d\n", *p1);

	const int* const p2 = &mum1;   //const 修饰 * 指针变量,p2 *p2 为只读
	//p2 = NULL;    //error
	//*p2 = 40;     //error
	printf("*p2 = %d\n", *p2);


}

int main()
{
	test19();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值