Hello all,
I want to remove the spaces " " from a specific string.
First I wrote this code:
Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string z=" hello";
string y;
for(int x=0;x<z.length();x++)
{
y=y+z[x];
}
cout<<y;
system("pause");
}
and it worked then I added an if statement to handle the " " in the z string.
I added the statement into the for loop.
Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string z=" hello";
string y;
for(int x=0;x<z.length();x++)
{
if(z[x]!=" ") y=y+z[x];
}
cout<<y;
system("pause");
}
But the compiler gave me this error!The error was in the new if statement I added.
error C2446: '!=' : no conversion from 'const char *' to 'int'
There is no context in which this conversion is possible
.\new.cpp(10) : error C2040: '!=' : 'int' differs in levels of indirection from 'const char [2]'
Can anyone please explain it and tell me how to overcome this problem.
Thank you.