函数原型:

char *strtok(char * strToken, const char *strDelimit)

参数说明:

strToken:源字符串,即待分割的串

strDelimit:strToken会根据这里的每个字符进行分割

返回值:

指向第一段被截取出来的字符串的指针,如果没有找到,则返回NULL。

调用说明:

(1)第一次调用strtok时,第一个参数是strToken。以后再调用时,第一个参数必须是NULL

(2)调用strtok后,源字符串会被修改

(3)strtok不是一个线程安全的函数

  1:    char str[] = "now # is the time for all # good men to come to the # aid of their country";
  2:    char delims[] = "#";
  3:    char *result = NULL;
  4:    result = strtok( str, delims );
  5:    while( result != NULL )
  6:    {
  7:        printf( "result is \"%s\"\n", result );
  8:        result = strtok( NULL, delims );
  9:    }  

 

 

这个函数的应用:

HDU 2526 和 HDU 1106

#include<cstdio>
#include<cstring>
#include<algorithm>

#define MAXN 1010

using namespace std;

char str[MAXN],*p;
int  num[MAXN];

int main()
{
	while (~scanf("%s",str))
	{
		int cnt=0;
		p=strtok(str,"5");
		while (p)
		{
			num[cnt++]=atoi(p);
			p=strtok(NULL,"5");
		}

		sort(num,num+cnt);

		for(int i=0;i<cnt;i++)
			if(i+1==cnt) printf("%d\n",num[i]);
			else		 printf("%d ",num[i]);

	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<cctype>

#define MAXN 150

using namespace std;

char str1[MAXN],str2[MAXN];


int main()
{
	int t;
	char *p;
	scanf("%d",&t);
	getchar();
	while (t--)
	{
		int cnt=0;
		gets(str1);
		p=strtok(str1," ");

		while (p)
		{
			str2[cnt++]=toupper(*p);
			p=strtok(NULL," ");
		}
		str2[cnt]=0;
		printf("%s\n",str2);
	}
	return 0;
}

关于strtok函数的更多相关文章

  1. shell脚本调用C语言之字符串切分之strtok函数

    今天上午在写一个需求,要求的比较急,要求当天完成,我大致分析了一下,可以采用从shell脚本中插入一连串的日期,通过调用proc生成的可执行文件,将日期传入后台数据库,在数据库中进行计算.需要切分日期 ...

  2. strtok函数 分类: c++ 2014-11-02 15:24 214人阅读 评论(0) 收藏

    strtok函数是cstring文件中的函数 strtok函数是cstring文件中的函数 其功能是截断字符串 原型为:char *strtok(char s[],const char *delin) ...

  3. strtok函数读写冲突问题

    先上测试代码 #include "stdafx.h" #include <iostream> using namespace std; int _tmain(int a ...

  4. 字符串函数之Strtok()函数

    Strtok()函数详解:   该函数包含在"string.h"头文件中 函数原型: char* strtok (char* str,constchar* delimiters ) ...

  5. STRTOK函数和STRTOK_R函数

    STRTOK函数和STRTOK_R函数 注:本文转载自博客园,感谢作者整理! 1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct pe ...

  6. popen strtok 函数的使用

    FILE * popen ( const char * command , const char * type ); int pclose ( FILE * stream );   type 参数只能 ...

  7. [转载]strtok函数和strtok_r函数

    1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct person{     char name[25];     char sex[1 ...

  8. 温故而知新-strtok函数

    温故而知新-strtok函数 记得之前没见过这个函数,是把字符串分割成更小的字符串 来个例子就是比较鲜明了 $string = "Hello world. Beautiful day tod ...

  9. 用strtok函数分割字符串

    用strtok函数分割字符串 需要在loadrunner里面获得“15”(下面红色高亮的部分),并做成关联参数. //Body response 内容: <BODY><; PRE&g ...

  10. lr中用strtok函数分割字符串

    需要在loadrunner里面获得“15”(下面红色高亮的部分),并做成关联参数. ,6,5,0,4,0,3,0,3,2,0,0,0,1 用web_reg_save_param取出“8,7,5,15, ...

随机推荐

  1. spring 对JDBC的支持 (8)

    目录 一.jdbc的简介 二.jdbcTemplate 的使用 2.1 maven 引入spring - jdbc ,c3p0 ,数据库mysql驱动 2.2 配置 数据源以及jdbcTemplate ...

  2. 在Windows Server2016中安装SQL Server2016(转)

    在Windows Server2016中安装SQL Server2016(转) 转自: http://blog.csdn.net/yenange/article/details/52980135 参考 ...

  3. CSS:百科

    ylbtech-CSS:百科 1.返回顶部 CSS (层叠样式表) 层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标 ...

  4. date和string转换用joda包

    import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import org.joda.time.format ...

  5. Linux中的网络管理——网络配置及命令

    Linux网络配置 在Linux中配置IP地址的方法有以下这么几种: 图形界面配置IP地址(操作方式如Windows系统配置IP,但在实际生产中,我们并不建议在我们的服务器上安装Linux的图形界面, ...

  6. 2-Ubuntu命令安装mysql服务器和客户端及安装后的简单验证操作

    转自: https://www.cnblogs.com/zhuyp1015/p/3561470.html 安装完成之后可以使用如下命令来检查是否安装成功:   sudo netstat -tap | ...

  7. 最长递增子序列nlogn的做法

    费了好大劲写完的  用线段树维护的 nlogn的做法再看了一下 大神们写的 nlogn  额差的好远我写的又多又慢  大神们写的又少又快时间  空间  代码量 哪个都赶不上大佬们的代码 //这是我写的 ...

  8. qt5.9.1 VS2017 qalgorithms.h

    qt5.9.1只有VS2017 64位支持, 在32位工程下会出现关于qalgorithms.h的错误,参考以下内容修改该头文件解决: https://codereview.qt-project.or ...

  9. http://localhost:8080 is requesting your username and password

    after you startup your tomcat,  you type a concrete request url  in broswer, the tomcat probably wil ...

  10. 2018-2-13-win10-edge扩展

    title author date CreateTime categories win10 edge扩展 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23 ...