Thread: Remove a specific char and remove multiple spaces

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Remove a specific char and remove multiple spaces

    Well right now this code Removes one specific letter from the string we give.
    The problem is that i found this func code on google so i don't really understand it(I am very new to this)
    I made the main's code myself.

    1)I need some help to understand what
    Code:
    string str = phrase;
    do???
    2)Why when i put
    Code:
    cout << str << endl;
    in main give me some random numbers??even if i change str with z, give me some other random numbers...
    3)THIS IS THE MOST IMPORTANT even if u can't help with others.
    I need to add in this code something that removes multiple spaces and leaves only ONE space.
    This program in the end should
    Take a String(Phrase) as an Input and a Letter to remove
    Remove Multiple Spaces and leave only 1 space between each word
    and give as the String(Phrase) with the specific letter removed and with only 1 space between each word.
    EXAMPLE:
    "What___a__great_____________weather__we have today" (consider underscore as a space because the forum doesn't let u put multiple spaces)
    and lets say we want to remove "e"
    SHOULD BECOME:
    "What a grat wathr w hav"




    Code:
    #include <cstdlib> 
    #include <iostream> 
    
    using namespace std; 
    
    
    int func(int i,char phrase[80]){ 
    string str = phrase; 
    char remove = i; 
    for(string::iterator it = str.begin(); it!=str.end(); ){ 
    if( (*it) == remove ) 
    it = str.erase( it ); 
    else 
    it++; 
    } 
    cout << str << endl; 
    } 
    
    
    int main(int argc, char *argv[]) 
    { 
    int i, n, z; 
    char phrase[80]; 
    printf("\nGive a Phrase "); 
    gets(phrase); 
    printf("\nThe Phrase is: "" %s\n"" ",phrase); 
    printf("\nGive the letter you want to be removed "); 
    scanf("%c",&i); 
    z=func(i,phrase); 
    
    system("PAUSE"); 
    return EXIT_SUCCESS; 
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    string str = phrase; is just another way of writing the constructor call string str(phrase); which means you build a string object and use phrase as an initial value. There are several things wrong with this post, but foremost is that all the important changes happen to the string object, which is a copy of phrase, not the phrase variable. You should decide what you are going to use and fix that.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    std::string RemoveChars(const std::string& Str, char Ch)
    {
        std::string out;
        bool first = true;
        bool space = false;
        for (std::string::const_iterator it = Str.begin(); it != Str.end(); ++it)
        {
            if (*it == ' ')
            {
                if (first == false)
                {
                    space = true;
                }
            }
            else if (*it != Ch)
            {
                if (space)
                {
                    out.push_back(' ');
                }
                out.push_back(*it);
                space = false;
                first = false;
            }
            else
            {
                space = false;
            }
        }
        return out;
    }
    Input: "___xxx_____teeestx__aaa___x____" (ch = 'x')
    Output: "teeest_aaa"
    Last edited by kmdv; 12-16-2010 at 10:41 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by stam
    3)THIS IS THE MOST IMPORTANT even if u can't help with others.
    I need to add in this code something that removes multiple spaces and leaves only ONE space.
    Taking this as-is, kmdv's example is wrong, because it removes all leading and trailing spaces, rather than reducing them to a single space. Personally, I would suggest something like this:
    Code:
    namespace
    {
        class IsConsecutiveSpace
        {
        public:
            bool operator()(char current, char previous) const
            {
                return previous == ' ' && current == previous;
            }
        };
    }
    
    std::string RemoveChars(const std::string& str, char ch)
    {
        using namespace std;
        string out(str);
        string::iterator end = remove(out.begin(), out.end(), ch);
        out.erase(unique(out.begin(), end, IsConsecutiveSpace()), out.end());
        return out;
    }
    But of course, based on your earlier two questions, you probably don't understand what this does either

    Basically, my idea is to make use of the std::remove algorithm to do exactly what you want: remove all the characters that match that one character provided. Then, I make use of the std::unique algorithm to remove consecutive spaces, leaving just one "unique" space.
    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

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by laserlight View Post
    Taking this as-is, kmdv's example is wrong, because it removes all leading and trailing spaces, rather than reducing them to a single space. Personally, I would suggest something like this:
    I wasn't getting deeply into his problem, since it would take more time than writing this entire function.
    About the validity: I think he wanted removing them as well, considering begin and end of string as whitespaces.

    EDIT:

    What is the function supposed to return for: x___test___x ?
    _test_ or test? (laserlight's returns the first one, my the second one).
    Last edited by kmdv; 12-16-2010 at 11:13 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Boost has string algorithms for find and replace (something sorely missing in the standard library). Perhaps you should give them a go?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    How is this problem find and replace? You don't replace anything.
    Last edited by whiteflags; 12-17-2010 at 12:41 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah well, seeing as the number of consecutive spaces are unlimited, it may not work very well (seems I missed that part). But if you have a fixed amount, it should work very well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Taking this as-is, kmdv's example is wrong, because it removes all leading and trailing spaces, rather than reducing them to a single space. Personally, I would suggest something like this:
    Code:
    namespace
    {
        class IsConsecutiveSpace
        {
        public:
            bool operator()(char current, char previous) const
            {
                return previous == ' ' && current == previous;
            }
        };
    }
    
    std::string RemoveChars(const std::string& str, char ch)
    {
        using namespace std;
        string out(str);
        string::iterator end = remove(out.begin(), out.end(), ch);
        out.erase(unique(out.begin(), end, IsConsecutiveSpace()), out.end());
        return out;
    }
    But of course, based on your earlier two questions, you probably don't understand what this does either

    Basically, my idea is to make use of the std::remove algorithm to do exactly what you want: remove all the characters that match that one character provided. Then, I make use of the std::unique algorithm to remove consecutive spaces, leaving just one "unique" space.

    Ok and how i am gonna give them the char a want to remove and the string i want to remove multiple spaces.
    Its not like common functions.
    Please explain i wanna learn something from this thing...

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Perhaps this is a bit more along the lines of basic processing. You can function-ize it.
    Code:
    #include <iostream>
    #include <string> 
    
    using namespace std ; 
    
    int main (int argc, char * const argv[]) {
    	string s("What   a  great             weather  we have today"); 
    	bool found ; 
    	string::size_type pos ; 
    	do { 
    		found = false ; 
    		cout << "string=" << s << endl ; 
    		if ((pos = s.find("  ")) != string::npos) {
    			found = true ; 
    			cout << "Found 2 blanks at pos " << pos << endl ; 
    			s.replace(pos, 2, " ") ; 
    		}
    		if ((pos = s.find("e")) != string::npos) { 
    			found = true ; 
    			cout << "string=" << s << endl ; 
    			cout << "Found 1 e at pos " << pos << endl ; 
    			s.replace(pos, 1, "") ; 
    		}
    	} while(found) ; 
    	cout << "String is >" << s << "<" << endl ; 
        return 0;
    }
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help wanted: removing multiple spaces from array
    By DHart07 in forum C++ Programming
    Replies: 13
    Last Post: 10-27-2010, 09:34 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM