Advertisement

01.20.2005 at 01:53PM PST, ID: 21282559
[x]
Attachment Details

Removing whitespace from inside the string

Asked by njava in C++ Programming Language

Tags: string, whitespace, remove

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(delims);
   str.erase(0,notwhite);

   // trim trailing whitespace
   notwhite = str.find_last_not_of(delims);
   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
[+][-]01.20.2005 at 01:56PM PST, ID: 13097996

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 01:58PM PST, ID: 13098014

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 02:27PM PST, ID: 13098323

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 02:30PM PST, ID: 13098359

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 02:32PM PST, ID: 13098377

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 02:38PM PST, ID: 13098443

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 02:55PM PST, ID: 13098578

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 03:17PM PST, ID: 13098778

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 03:21PM PST, ID: 13098799

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C++ Programming Language
Tags: string, whitespace, remove
Sign Up Now!
Solution Provided By: guitaristx
Participating Experts: 6
Solution Grade: A
 
 
[+][-]01.20.2005 at 03:26PM PST, ID: 13098844

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 03:33PM PST, ID: 13098896

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 03:50PM PST, ID: 13099011

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 04:05PM PST, ID: 13099092

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 05:21PM PST, ID: 13099419

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 05:25PM PST, ID: 13099442

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 10:15PM PST, ID: 13100498

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2005 at 10:22PM PST, ID: 13100529

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.22.2005 at 12:03AM PST, ID: 13109405

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.08.2005 at 02:55PM PST, ID: 13259648

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.24.2005 at 07:50AM PST, ID: 13393668

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.24.2005 at 07:53AM PST, ID: 13393717

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32