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

posted @ 2012-05-15 15:29  byfei  阅读(473)  评论(0编辑  收藏  举报