• 去掉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
     

  • 相关阅读:
    VS2013 使用QCustomPlot等三方库如何配置
    error LNK1112:模块计算机类型"X64" 与目标计算机类型"X86" 冲突
    Qt 获取屏幕当前分辨率
    流程控制
    导航条和工作内容纪要
    js简介
    高度塌陷
    浮动
    文档流
    display和overflow
  • 原文地址:https://www.cnblogs.com/byfei/p/3112257.html
Copyright © 2020-2023  润新知