学习代码:
#include<stdio.h>
void test01();//squeeze numbers
void text02();//squeeze numbers
void test03();//print front[] words
void test04();//match words_1stVersion
void test05();//match words_2stVersion
void test06();//match ^[x1-x2]
void test07();//cut string out using its format
void test08();//take the useful information string
void test09();//practice
int main()
{
test01();
test02();
test03();
test04();
test05();
test06();
test07();
test08();
test09();
return 0;
}
void test01()
{
char *ch = "123hello";
char buf[1024];
sscanf(ch,"%*d%s", buf);
printf("1%s",buf);
}
void test02()
{
char *ch = "hello123";//or add \t or space between o and 1, but not suggested!
char buf[1024];
sscanf(ch, "%*[a-z]%s", buf);
printf("2%s", buf);
}
void test03()
{
char *ch = "123hello";
char buf[1024];
sscanf(ch,"%6s", buf);
printf("3%s", buf);
}
void test04()
{
char *ch = "123abcdecccfg";
char buf[1024];
sscanf(ch,"%*d%[a-c]", buf);
printf("4%s", buf);
}
void test05()
{
char *ch = "aa22bbccdefg123";
char buf[1024] = {0};
sscanf(ch, "%[abc]", buf);//if it is not match successfully, then just stop matching
printf("5%s", buf);
}
void test06()
{
char *ch = "123abcdefg";
char buf[1024];
sscanf(ch,"%[^a-z]", buf);
printf("6%s", buf);
}
void test07()
{
char *ch = "219.231.164.200";
int a, b, c, d;
a = b = c = d = 0;
sscanf(ch, "%d.%d.%d.%d", &a, &b, &c, &d);
printf("7%d %d %d %d", a, b, c, d);
}
void test08()
{
char *ch = "12345#liuwenhan$1114068569";//take my name
char buf[1024];
sscanf(ch,"%*[^#]#%[^$]", buf);
printf("8%s", buf);
}
void test09()
{
char *ch = "Helloworld@itcast.cn";
char buf1[1024];
char buf2[1024];
sscanf(ch,"%[^@]", buf1);
sscanf(ch, "%*[^@]@%s", buf2);
printf("9%s\n%s", buf1, buf2);
}