zoukankan      html  css  js  c++  java
  • 关于strtok函数

    函数原型:

    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"
    ", 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
    ",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
    ",str2);
    
    	}
    
    	return 0;
    
    }
    
  • 相关阅读:
    C++中整型变量的存储大小和范围
    A1038 Recover the Smallest Number (30 分)
    A1067 Sort with Swap(0, i) (25 分)
    A1037 Magic Coupon (25 分)
    A1033 To Fill or Not to Fill (25 分)
    A1070 Mooncake (25 分)
    js 获取控件
    C#代码对SQL数据库添加表或者视图
    JS 动态操作表格
    jQuery取得下拉框选择的文本与值
  • 原文地址:https://www.cnblogs.com/gt123/p/3689290.html
Copyright © 2011-2022 走看看