Results 1 to 14 of 14

Thread: Trim Function in C/VC++

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Trim Function in C/VC++

    Can anyone show me the available Trim function in C/VC++ or any equavalent function

    regards,

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Dunno if there is one, here's something i made just for you
    PHP Code:
    void Trim(char*,char*,char*);
    void Trim(charinfirstcharinlast,charout){
        for(;(
    infirst<inlast)&&(*inlast==0);--inlast);
        for(;(
    infirst<inlast)&&(*infirst==32);++infirst);
        for(;(
    infirst<inlast)&&(*inlast==32);--inlast);
        for(;(
    infirst<=inlast);out++)*out=*infirst++;
        *
    out=0;
    }


    int main(int argccharargv[])
    {
        
    char text[8]=" Chris ";
        
    char text2[8];
        
    Trim(&text[0],&text[7],&text2[0]);
        
    cout << text2;
        return 
    0;

    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.

  3. #3

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    Thx Keda for you prompt reply. Mean while, I also get some sample code from PlanetSouceCode

    PHP Code:
    wchar_tLTrim(wchar_ts)
    {
        
    //**************************************
        //     
        // Name: strrtrim
        // Description:Removes trailing spaces from the end of a string. See "trim" (Rename to "LTrim") to remove leading spaces.
        // By: Man In Limbo
        //
        // Inputs:the string
        //
        // Returns:the trimmed string
        //
        // This code is copyrighted and has// limited warranties.Please see [url]http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.233/lngWId.3/qx/vb/scripts/ShowCode.htm[/url] ::for details.
        //     
        //**************************************
        
        // strrtrim : removes trailing spaces from the end of a string
        
    int i;

        if (
    s
        {
            
    wcslen(s);
            while ((--
    i)>&& iswspace(s[i]) ) s[i]=0;
        }
        return 
    s;
    }

    wchar_tRTrim(wchar_ts)
    {
        
    //**************************************
        //     
        // Name: trim
        // Description:Strips spaces from front of string. Also see strrtrim (Rename to "LTrim")to remove the spaces off the end.
        // By: Man In Limbo
        //
        // Inputs:The String
        //
        // Returns:The Trimmed String.
        //
        // This code is copyrighted and has// limited warranties.
        // Please see [url]http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.232/lngWId.3/qx/vb/scripts/ShowCode.htm[/url] ::for details.
        //     
        //**************************************

        // trim : scan over spaces from the front of a string.

        
    if (s)
        {
            while (
    iswspace(*s)) s++; 
        }

        return 
    s;

    regards,

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    that's nullchars not spaces, besides you can hardly use them in conjunction to function as Trim.
    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.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Is this allowed?
    Code:
    tstring Trim(tstring sIn) {
    	int i, start = -1, end = -1;
    
    	for(i = 0; i < sIn.length(); i++) {
    		if(_istspace(sIn[i])) start = i;
    		else {
    			sIn = sIn.substr(i, sIn.length() - i);
    			break;
    		}
    	}
    
    	for(i = sIn.length()-1; i >= 0; i--) {
    		if(_istspace(sIn[i])) end = i;
    		else {
    			sIn = sIn.substr(0, i+1);
    			break;
    		}
    	}
    	
    	return sIn;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Dunno, but looks very heavy to me :/
    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.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A bit, yeah. The advantage is that it works a treat, stripping off newlines, tabs, and spaces And it calls .substr a maximum of twice
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    oh yeah and it's so heavy it makes me throw up
    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.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Git

    If I could really be bothered I might write a faster one
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    hehe, you should learn to use the power in C++
    May the Force be with you
    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.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Speed wasn't a priority in that one, but I accept your challenge
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    I think those written app with C++ is 'coz of the speed & power of control everthing. Juz like me choose VC++ to write app in WinCE.

    by the way, thanks for both of you sample code & it help very much.

    Originally posted by parksie
    Speed wasn't a priority in that one, but I accept your challenge

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    tell me one thing kedaman
    why do you keep using the ascii codes of the letters. It's not as if ' ' or '\0' was any slower (resolved by the compiler) and it is far more readable.

    All the buzzt
    CornedBee

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    kinda old habit since i used vb
    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.

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