Thread: remove redundant white spaces in string

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    22

    remove redundant white spaces in string

    Hi, is a strange request. I don't need a code. could somebody please explain me how it has to be done step by step. I just need the logic in for task.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are "redundant white spaces", and what algorithm have you in mind?
    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

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    lets say i have a string

    Code:
    "this          is    a                 string     with     redundant                                               spaices       in          it"
    I don't have any algo in my mind yet

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ph3s
    lets say i have a string
    All the white space in that string look pretty essential to me

    What I am asking for is a definition of "redundant white space", and also an explicit statement of what does it mean to remove them. For example, maybe given your example string, your desired result is:
    Code:
    "this is a string with redundant spaices in it"
    or maybe it is:
    Code:
    "thisisastringwithredundantspaicesinit"
    or maybe the string contain tab characters followed by space characters, and you only want to keep the first white space character, so you only keep the tab characters. Or maybe...
    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
    Nov 2011
    Posts
    22
    O, I see. My bad. I meant if more than one white space among two words = redundancy.

    Answer A in your assumptions
    Last edited by ph3s; 03-18-2012 at 04:20 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, I give up. I guess if you don't want to be precise, then you're just going to have a hard time solving problems because you won't be able to think clearly

    Anyway, here's one way to solve your problem, more or less:
    Code:
    r = 0
    w = 0
    while string[r] is not a null character
        string[w] = string[r]
        increment r
        if string[w] is a white space character
            while string[r] is a white space character
                increment r
        increment w
    string[w] = null character
    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

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    damn what I have done to curse me like that? Anyway thank you for hint and help.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ph3s
    what I have done to curse me like that?
    Hey, it is just a conditional observation, not a curse
    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

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    But I did exlained so you can see. Look at your post and examples. Which particular example you typed first?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ph3s View Post
    But I did exlained so you can see. Look at your post and examples. Which particular example you typed first?
    Maybe laserlight was a little stressed out Anyway, the pseudo code in post #6 is a good starting place for what you want to do. Try applying that and if you have a problem post you code.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    We are all getting stressed from time to time, like me now. I just don't follow in this part of pseudo code.

    Code:
    r = 0//got it
    
    w = 0/got it
    
    while string[r] is not a null character // that is ok
    
        string[w] = string[r]//yep all the steps to copy one
    
        increment r//string
    
        if string[w] is a white space character // right, lets say it meets '  '
    
            while string[r] is a white space character//here I'm stack
    
                increment r //what for ?
    
        increment w// to another
    
    string[w] = null character // well yes, it has to end somewhere
    Last edited by ph3s; 03-18-2012 at 11:30 AM.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The reason you increment r is to, well, skip the whitespace!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If there is whitespace eliminated, the resulting string should be shorter than it started out as. Laserlight's algorithm works in-place (meaning, the input string is modified), so w represents the current index in the new string, and r the current index in the old string (it will be ahead of any changes made, then at the end the new version is null terminated).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    Thanks gens and ladies. Now I can see. is Anythink looks wrong?

    Code:
    main()
    {
        char redundant[]= "here is  a    messy  string";
        char proper[64];
        int x=0, y=0;
    
        while ( redundant[x] != '\0')
        {
            proper[y]=redundant[x];
    
            x++;
    
            if (proper[y] == ' ')
            {
                while (redundant[x]== ' ')
                    x++;
            }
    
            y++;
        }
    
    
        proper[y]='\0';
    
        printf("%s",proper);
    
    }
    Last edited by ph3s; 03-18-2012 at 12:04 PM.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ph3s View Post
    Thanks gens and ledies. Now I can see. is Anythink looks wrong?
    That should work. Just make sure you test it enough to be sure.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove a specific char and remove multiple spaces
    By stam in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2010, 07:50 AM
  2. How to remove end spaces from string
    By nitinmhetre in forum C Programming
    Replies: 4
    Last Post: 12-16-2006, 01:14 AM
  3. Replies: 1
    Last Post: 03-08-2005, 12:02 PM
  4. white spaces
    By Jules in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 02:55 PM
  5. string input...with white spaces?
    By Pureghetto in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 01:52 PM