Nayak Nayak - 1 month ago 6
C++ Question

I have written a function to remove spaces from a sentence but its only taking chars before the space & not considering the entire sentance

I have written a function to remove spaces from a sentence but its only taking chars before the space & not considering the entire sentence.
if I run this Out put is

Enter the string with space/ tab to trim
IamDoing very fine
Length of the String is : 8
IamDoing


I what changes I need to do get "IamDoingveryfine"
Please help, Code is as follows..."

void removeSpace(const string &str)
{
string tempStr;
string p_str = str;
int l_length = p_str.length();
cout<<"Length of the String is : "<<l_length<<endl;
for (int i=0; i<l_length; i++)
{
if(p_str[i] != ' ' || p_str[i] != '\t')
{
tempStr +=p_str[i]; //to add chars into tempstr
}
}
cout<<tempStr<<'\n';
return ;
}

Answer

The problem is not with the removespace(), problem is when you are taking the input.

It should be like this:

std::string line;
std::cout << "Enter the string with space/ tab to trim" << std::endl;
std::cin.getline (line);