Results 1 to 22 of 22

Thread: Remove spaces from a string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Remove spaces from a string

    Hi,
    How can I remove spaces from strings?
    I'm talking about good old C not C++.

    Is there a built in function or do I have to write my own?

    Please provide a sample.
    Thanks

    Tomexx.

  2. #2
    Sc0rp
    Guest
    This works:
    PHP Code:
    #include <iostream.h>
    #include <string.h>

    int main()
    {
        
    char myStr[] = "Blah blah yadda packa.";
        
    unsigned int myLen strlen(myStr);

        for (
    unsigned int i 0myLeni++)
        {
            if (
    myStr[i] == ' ')
            {
                for (
    unsigned int j imyLenj++)
                    
    myStr[j] = myStr[j+1];
            }
        }

        
    cout << myStr << endl;

        return 
    0;


  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    way too heavy

    PHP Code:
    #include <iostream.h>
    #include <string.h>

    int main()
    {
        
    char myStr[] = "Blah blah yadda packa.";
        
    chari=myStr;
        
    charl=i+sizeof(myStr);

        for (
    charj=i;i<l;*j=*++i)j+=(*i!=32);
        *
    j=0;

        
    cout << myStr << endl;

        return 
    0;

    Last edited by kedaman; Aug 22nd, 2001 at 01:29 PM.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Sc0rp
    Guest

    WOW!

    Cool solution but hard to understand.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    it definitely kicks ass
    It does a single loop, with two variables,one dropping the spaces and on going straight trough assigning the earlier one.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    Thanks guys,
    Kedaman, your code doesn't seem to work.

    Could you take a look at it again and make it into a function that accepts the string as the argument.
    Also where do you initialize the 'j' variable?

    I want to be able to call it like:

    Code:
    myString[] = "Bla Bla Bla "
    Trim(myString)
    
    
    void Trim(char* szStrToCheck)
    {
    
    
    return;
    }
    Thanks

    Tomexx.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    PHP Code:
    #include "stdafx.h" //maybe it's because this wasnt included
    #include <iostream.h>
    #include <string.h>

    void Trim(char*,char*);

    int main()
    {
        
    char myStr[] = "Blah blah yadda packa.";
        
    Trim(myStr,myStr+sizeof(myStr));
        
    cout << myStr << endl;

        return 
    0;
    }

    void Trim(chari,charl)
    {
        for (
    charj=i;i<l;*j=*++i)j+=(*i!=32);
        *
    j=0;

    okay but you need to pass the termination pointer as well
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    BTW,
    I'm compiling the program on a VAX VMS compiler.
    It gave me an error here:

    '*j=*' saying that this is an obsolete statement or something

    I'll try your new code.
    Thanks

    Tomexx.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    *j=*++i;
    'tis suppose to increment i pointer and assign it's contents to j's, maybe it'll work if you put parentesis around like this:
    *j=*(++i);
    but it's perfectly legal to do this in C++ so i wonder what kind of compiler wouldn't understand it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    I'm not using C++

    I'm using an OLD C compiler that's not even on PC but on VAX.
    Thanks

    Tomexx.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Ah, but that still shouldn't be a problem, I don't know C but the syntax of dereferencing shouldn't differ from C++
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    Sc0rp
    Guest
    Hmmm.. Tomexx, have you tried my code?
    Does it work?

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    Sc0rp,
    Yes I did try it and it almost worked. There was a slight problem though, but I'll give you more details tomorrow (the program is at work).
    Thanks

    Tomexx.

  14. #14
    Sc0rp
    Guest
    Tomexx:

    Do you want the code to remove ALL the spaces from a string or only the left-most and right-most spaces?

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    If thats the case i've posted my solution for it here:
    http://www.vbforums.com/showthread.p...threadid=97909
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    Sc0rp
    Guest
    I tried my code on various strings and it works perfectly.
    The problem could only be that it doesn't achieve your goal.

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It's awfully slow though I think it's the compiler
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  18. #18
    Sc0rp
    Guest
    I found a flaw in my program.
    It wouldn't remove multiple spaces that appear next to each other.
    I fixed it and I also wrapped it in a function. :
    PHP Code:
    #include <iostream.h>
    #include <string.h>

    int RemoveSpaces(char*, const char*);
    unsigned int GetStrLen(const char*);

    // Main program
    int main()
    {
        
    char myStr[] = " This   is a string   with a lot  of spaces. . ... ";
        
        
    charnewStr = new char[GetStrLen(myStr)];
        
    RemoveSpaces(newStrmyStr);

        
    cout << newStr << endl;

        
    delete[] newStr;
        return 
    0;
    }

    // Removes all space from a string
    int RemoveSpaces(charnewStr, const charoldStr)
    {
        
    unsigned int theLen strlen(oldStr);
        
    strcpy(newStroldStr);

        for (
    unsigned int i 0theLeni++)
        {
            while (
    newStr[i] == ' ')
            {
                for (
    unsigned int j itheLenj++)
                    
    newStr[j] = newStr[1];
            }
        }

        return 
    0;
    }

    // Returns the length of the string passed to it including the null char
    unsigned int GetStrLen(const chartheStr)
    {
        return (
    strlen(theStr) + 1);

    It is possible to change the function so it changes the original string passed to it, if you want.
    Anyway, bug fixed.

  19. #19
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It's still freaking heavy
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  20. #20
    Sc0rp
    Guest
    I know that.
    It's not a contest kedaman. Tomexx can choose the code that fits him best.

  21. #21
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    you're right, he'll end up choosing code that someone codes in C for him
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  22. #22
    Sc0rp
    Guest

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width