Thread: remove whitespace program won't work

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    5

    remove whitespace program won't work

    The program is supposed to erase all whitespace and all chars past the whitespace from a string, but it just hangs the terminal.
    Code:
    #include<iostream>
    #include<string>
    #include<string.h>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    
    
    #include<sstream>
    #include<fstream>
    #include<list>
    #include<numeric>
    #include<map>
    #include<iterator>
    
    
    void removespaces(vector<string>& v){
        for(int i=0; i<v.size(); ++i){
            string::iterator it=find(v[i].begin(),v[i].end(),' ');
            if(it!=v[i].end()){
                v.push_back(v[i]);
                it=find(v[i].begin(),v[i].end(),' ');
                v[i].erase(it,v[i].end());
            }
        }
    }
    int main()
    {
    
    
        vector<string> words;
    
    
    
    
        words.push_back("aa bb");
    
    
        removespaces(words);
    
    
        for(auto& f:words)
            cout<<f<<'\n';
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you need the vector of strings if your aim is to remove whitespace from a single string?

    I'd guess that you either want a function like this:
    Code:
    void remove_spaces(std::string& s);
    Or you want a function like this:
    Code:
    std::vector<std::string> break_into_words(const std::string& s);
    The former removes whitespace from a single string, so "aa bb" will become "aabb"; the latter breaks up a string into "words" based on whitespace boundaries and returns the result as a vector of word strings, so "aa bb" becomes "aa" and "bb".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove trailing whitespace
    By whiskybrah in forum C Programming
    Replies: 7
    Last Post: 07-10-2018, 10:43 AM
  2. Remove Signs & Whitespace (w/pointer)
    By coffee_cup in forum C Programming
    Replies: 11
    Last Post: 11-09-2012, 03:43 PM
  3. Need this program to skip leading whitespace using isspace()
    By steals10304 in forum C Programming
    Replies: 6
    Last Post: 10-12-2009, 03:55 PM
  4. How to take a string input and remove whitespace?
    By Jasonx521 in forum C Programming
    Replies: 5
    Last Post: 10-06-2006, 10:24 PM
  5. Replies: 10
    Last Post: 06-20-2006, 03:07 PM

Tags for this Thread