Thread: strtrim

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    strtrim

    Hello guys

    In visual c++, we can use StrTrim function with including shlwapi.h library for triming character from String.

    Is there any function available on linux platform in c++ for the same purpose.

    P.S: According to this webpage , If I include publib.h library, I can use StrTrim function but didn't work..

    Thanks for your help

    Best regards

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't know of any that will do it for you, but it shouldn't be that hard to come up with one. Are you using C style strings or C++ strings?

    >> but didn't work.
    How did it not work?

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Based on your description, I don't think strtrim is implemented in Standard C/C++. If it doesn't do anything really fancy, you could try rolling your own.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Quote Originally Posted by Daved
    Are you using C style strings or C++ strings?
    I am using C style strings like
    Code:
    char line[500];
    I know It shouldn't be hard to write one but I don't want my program has to many lines. Also, linux should have this function in its library because it is very usufull function.


    Quote Originally Posted by Daved
    >> but didn't work.
    How did it not work?
    When I included publib.h I got the following error:

    Code:
    strim.cpp:10: error: `strtrim' was not declared in this scope
    I think I should use visual c++ for this program.

    Thanks

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Here is a string version for fun...
    Code:
    #include <iostream>
    #include <string>
    
    void StrTrim(std::string& buffer, const std::string& trimChars)
    {
        std::string::size_type first = buffer.find_first_not_of(trimChars);
        std::string::size_type last = buffer.find_last_not_of(trimChars);
        if (first == std::string::npos)
            buffer.clear();
        else if (first <= last)
            buffer = buffer.substr(first, (last+1)-first);
    }
    
    int main()
    {
        std::string buffer("_!ABCDEFG#");
        std::string trim("#A!_\0");
    
        std::cout  <<  "The string before calling StrTrim: ";
        std::cout  <<  buffer;
        std::cout  <<  "\n";
    
        StrTrim(buffer, trim);
    
        std::cout  <<  "The string after calling StrTrim: ";
        std::cout  <<  buffer;
        std::cout  <<  "\n";
    }
    Edit: Since you are using C style strings that won't work as is (although it would be close). Do you have publib.h on your system? If you open it, is there a declaration for strtrim in there?
    Last edited by Daved; 09-22-2006 at 10:14 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I know It shouldn't be hard to write one but I don't want my program has to many lines.
    That's not a good reason. Typically, people who want to minimize the number of lines are the ones who have trouble grokking longer programs. The problem with this is that running and hiding doesn't make your program any better, and often makes it worse.

    >Also, linux should have this function in its library because it is very usufull function.
    And C++ should have GUI support in the standard library because GUIs are useful. What you think is useful may not be useful enough to be in a system library. It's just that simple.

    >When I included publib.h I got the following error:
    Look in the header and see if the function is there. If it's not, tough luck. If it is, you're probably spelling the name incorrectly.

    >I think I should use visual c++ for this program.
    Wait a minute. Is your program Linux or Windows based? If it's portable enough to switch your implementation system on a whim then you shouldn't be relying on non-standard libraries to begin with. Especially if the function in question is easily written.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed