hdu1106 字符串水题strtok()&&strchr()&&sscanf()+atoi()使用

字符串的题目 用库函数往往能大大简化代码量

以hdu1106为例

函数介绍

strtok()

原型char *strtok(char s[], const char *delim);

功能:

分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
例如:strtok("abc,def,ghi",","),最后可以分割成为abc def ghi.尤其在点分十进制的IP中提取应用较多。
(注意delim里面的不能看成一个整体,比如“ab" 实际上当'a' ‘b'处理的即(”bba“)也会全部被切割掉成NULL));
奉劝最好只有一个字符做参数 ,中间过程不改变参数。否则可读性很差;

说明


strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针
使用示范
#include<string.h>
#include<stdio.h>
    int main(void)
 {
    charinput[16]="abc,d";
       char*p;
     p=strtok(input,",");
     if(p)  printf("%s
",p);
     p=strtok(NULL,",");
     if(p) printf("%s
",p);
     return 0;
}

atoi()



功能:

字符串转换成整型数。ASCII to integer 的缩写。

说明


头文件: #include <stdlib.h>
参数nptr字符串,如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 ) 字符时停止转换,返回整型数。否则,返回零,

还有 转换为浮点数的atof() strtol() strtod() strtold() strtol() 这在文章结束后介绍

Hdu1106代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int num[1010];
char temp[1010];
int Qsort(int s,int t)
{
	int i=s,j=t,x=num[s];
	while(i<j)
	{
		while(i<j&&num[j]>x) j--;
		if(i<j) num[i]=num[j],i++;
		while(i<j&&num[i]<x) i++;
		if(i<j) num[j]=num[i],j--;
	}
	num[i]=x;
	if(s<i-1) Qsort(s,i-1);
	if(i+1<t) Qsort(i+1,t);
	return 0;
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	char *k;
	int len,i;
	while(gets(temp)!=NULL)
	{
		k=NULL;len=0;
		for(k=strtok(temp,"5");k!=NULL;k=strtok(NULL,"5"))
			num[len++]=atoi(k);
		Qsort(0,len-1);
	
	for(i=0;i<=len-1;i++)
	{
		printf("%d",num[i]);
		if(i!=len-1) printf(" ");
	}
	printf("
");
	}
		return 0;
}


strchr()

说明

char *strchr(const char* _Str,int _Val)
char *strchr(char* _Str,int _Ch)
头文件:#include <string.h>
功能:查找字符串s中首次出现字符c的位置
说明:返回首次出现c的位置的指针,返回的地址是字符串在内存中随机分配的地址再加上你所搜索的字符在字符串位置,如果s中不存在c则返回NULL
返回值:成功则返回要查找字符第一次出现的位置,失败返回NULL


sscanf()


说明


函数原型:
int sscanf( const char *, const char *, ...);
int sscanf(const char *buffer,const char *format,[argument ]...);
buffer存储的数据
format格式控制字符串
argument 选择性设定字符串
sscanf会从buffer里读进数据,依照format的格式将数据写入到argument里。
特殊用法:
1. 常见用法。
1
2
3
charbuf[512];
sscanf("123456","%s",buf);//此处buf是数组名,它的意思是将123456以%s的形式存入buf中!
printf("%s ",buf);
结果为:123456
2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。
1
2
sscanf("123456","%4s",buf);
printf("%s ",buf);
结果为:1234
3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。
1
2
sscanf("123456abcdedf","%[^]",buf);
printf("%s ",buf);
结果为:123456
4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
1
2
sscanf("123456abcdedfBCDEF","%[1-9a-z]",buf);
printf("%s ",buf);
结果为:123456abcdedf
当输入:  sscanf("123456abcdedfBCDEF","%[1-9A-Z]",buf);
1
printf("%s ",buf);
结果为:123456BCDEF
5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。
1
2
sscanf("123456abcdedfBCDEF","%[^A-Z]",buf);
printf("%s ",buf);
结果为:123456abcdedf
6、给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,
先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中
1
2
sscanf("iios/12DDWDFF@122","%*[^/]/%[^@]",buf);
printf("%s ",buf);
结果为:12DDWDFF
7、给定一个字符串“hello, world”,仅保留world。
(注意:“,”之后有一空格,%s遇空格停止,加*则是忽略第一个读到的字符串)
1
2
sscanf(“hello,world”,"%*s%s",buf);
printf("%s ",buf);
结果为:world
%*s表示第一个匹配到的%s被过滤掉,即“hello,”被过滤了
如果没有空格则结果为NULL。

原文地址:https://www.cnblogs.com/zy691357966/p/5480490.html