zoukankan      html  css  js  c++  java
  • 去掉string中的空格

    void str(char * a)
    {
     char *toks = " ";
     char * tok = strtok(a, toks);
     while (tok)
     {
      if (tok == a)
       strcpy(a, tok);
      else
       strcat(a, tok);
      tok = strtok(NULL, toks);
     }
    }

    int main()
    {

     string b = "this is a dog";
     str(const_cast<char*>(b.c_str()));
     cout<<b;

     return 0;
    }

      

    #include <iostream>   
    #include <cstring>   
    using namespace std;
    int main() 
    { 
    	char sentence[]="This is a test with strtok"; 
    	char *tokenPtr=strtok(sentence," "); 
    	while(tokenPtr!=NULL) 
    	{ 
    		cout<<tokenPtr; 
    		tokenPtr=strtok(NULL," "); 
    	} 
    	getchar();
    	return 0;
    } 
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    void Delete(char * a)
    {
    	char * tok = strtok(a, " ");
    	while (tok!=NULL)
    	{
    		if (tok == a)
    			strcpy(a, tok);
    		else
    			strcat(a, tok);
    		tok = strtok(NULL, " ");
    	}
    }
    
    int main()
    {
    	char sentence[]="This is a test with strtok"; 
    	Delete(sentence);
    	cout<<sentence;
    	getchar();
    	return 0;
    }


     

    #include <stdio.h>
    #include <string>
    #include <iostream>
    using namespace std;

    string DelNull(string str)
    {
     char *buf=const_cast<char*>(str.c_str());
     char *token;
     string strData;
     while ((token = strsep(&buf, " ")) != NULL)
      strData += token;
     return strData;
    }

    int main(void)
    {
     string str = "  root x艾迪  0 root /root /bin/bash ";
     cout<<DelNull(str);
     return 0;
    }

    扩展内容:

    http://www.cnblogs.com/longzhao1234/archive/2012/05/31/2528317.html
     

  • 相关阅读:
    Bellman算法
    Codeforces Round #378 (Div. 2) D
    运算符优先级
    Kruskal算法
    Java 大数运算
    无根树转有根树
    欧拉函数模板
    HDU 4135 Co-prime(容斥原理)
    快速求n的质因子(数论)
    Markdown中插入数学公式
  • 原文地址:https://www.cnblogs.com/byfei/p/3112257.html
Copyright © 2011-2022 走看看