Friday, July 31, 2009

.Program in c++ to remove all spaces in a string. e.g. "love you all" should look like "loveyouall"

The string "loveyouall" should be stored in the same string and not in new one.

.Program in c++ to remove all spaces in a string. e.g. "love you all" should look like "loveyouall"
This should work:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;


void stripSpace(string %26amp;);


int main() {


string test("HKLM\ Software\Microsfot\ Windows\Current Version\Run");


stripSpace(test);


cout %26lt;%26lt; test %26lt;%26lt; endl;


return 0;


}


void stripSpace(string %26amp;str) {


for (int i=0;i%26lt;str.length();i++)


if (str[i]==' ') {


str.erase(i,1);


i--;


}


}





Enjoy :)
Reply:If u ve programmed in Turbo C++ this might be helpful..





#include %26lt;iostream.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;stdio.h%26gt;


int main(void)


{


char s[50];


cout %26lt;%26lt;"\nEnter String to Truncate: ";


gets(s);


int len = strlen(s),j=0;


for(int i=0;i%26lt;len;i++)


{


if(isspace(s[i]))


{


j=i;


for(;j%26lt;len-1;j++)


s[j] = s[j+1];


if(i!=j)


{


s[j] = '\0';


len--;


}


}


}


cout %26lt;%26lt;"\n\nAfter Trunc: "%26lt;%26lt; s;


}


No comments:

Post a Comment