03 02scanf:%*d%s;空格 \n \t;[ab]、[ ^ab]、[a-z]、[ ^a-z]

ssanf(source,"字符匹配",target)
sscanf,匹配从第一个字符开始

%*d%s		非数字,字符。
	情况:
	1、乱码,数据类型不是%s;
	2、空白,没有间隔符(空格、\t、\n等)
	
%[abc]		[abc]
%[^abc]		[非abc]
%[a-z]		a到z
%[^a-z]		非[a到z]

01 %*d%s

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void test1() {
	char* str = "12345abcde";
	char buf[1024] = { 0 };
	sscanf(str, "%*d%s", buf);//abcde
	printf("%s", buf);
}


void main() {
	test1();

	system("pause");
}

02 空格 \n \t

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void test2() {
	char* str1 = "abcde12345";
	char* str2 = "abcde 12345";
	char* str3 = "abcde\t12345";
	char* str4 = "abcde\n12345";

	int buf1[1024] = { 0 };
	char buf2[1024] = { 0 };
	char buf3[1024] = { 0 };
	char buf4[1024] = { 0 };
	char buf5[1024] = { 0 };
	
	sscanf(str1, "%*s%d", buf1);//乱码,类型不对应
	sscanf(str1, "%*s%s", buf2);//空白,匹配不上,要有空格、\t、\n等间隔符
	sscanf(str2, "%*s%s", buf3);//12345
	sscanf(str3, "%*s%s", buf4);//12345
	sscanf(str4, "%*s%s", buf5);//12345

	printf("%d\n", buf1);
	printf("%s\n", buf2);
	printf("%s\n", buf3);
	printf("%s\n", buf4);
	printf("%s\n", buf5);
}


void main() {
	test2();

	system("pause");
}

03 [ab]、[ ^ab]、[a-z]、[ ^a-z]

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void test3() {//% [a - z]s
	char* str = "abcde12345";
	char buf1[1024] = { 0 };
	char buf2[1024] = { 0 };
	char buf3[1024] = { 0 };
	char buf4[1024] = { 0 };

	sscanf(str, "%[a-z]s", buf1);//abcde
	sscanf(str, "%[^a-z]s", buf2);//空白 
	sscanf(str, "%[ab]s", buf3); //ab
	sscanf(str, "%[^ab]s", buf4);//空白

	printf("%s\n", buf1);
	printf("%s\n", buf2);
	printf("%s\n", buf3);
	printf("%s\n", buf4);

}

void main() {
	test3();
	system("pause");
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值