Here is my string:
A B(C) D
I wrote a trim function that removes leading and trailing whitespace.
After applying trim, I get a string like this:
A B(C) D
I have problems deleting whitespace (space and tabs) from that string afterwards.
The resulting string should be:
AB(C)D
string trim(string str)
{
char const* delims = " \t\r\n";
int pos;
// trim leading whitespace
string::size_type notwhite = str.find_first_not_of(deli
ms);
str.erase(0,notwhite);
// trim trailing whitespace
notwhite = str.find_last_not_of(delim
s);
str.erase(notwhite+1);
//delete whitespace inside the instruction
//----I need help here
while (str.find(" "))
{
pos = str.find(" ");
str=str.substr(pos);
}
//-------------trim is used in openFile function------
int openFile(string filename)
{
string line;
ifstream sin(filename.c_str(), ios::in);
if (!sin) {
cerr << "Can't open " << filename;
return 2;
}
while(getline(sin,line)){
//Calculate a number of lines
line_count++;
//remove whitespaces
line = trim(line);
//skip empty lines
if (line.empty())
continue;
cout << line << endl;
}
cout << "Number of lines: " << line_count << endl;
return 0;
}
Start Free Trial