Thread: Trim

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Trim

    Code:
    #include <stdio.h>
    
    char *trim( char *array )
    {
        int i;
        
        for( ; *array == ' '; array++ );
        
        i = strlen( array );
        
        while( (--i > 0) && (array[i] == ' ') )
            array[i] = 0;
        
        return array;
    }
    
    int main()
    {
        char a[] = "    hello world    ";
        
        trim( a );
        
        printf("%s", a);
        printf("!");
        
        printf("\n\n%s!", trim(a));
        
        return 0;
    }
    Do you guys see how my function doesn't change the string completely? It will really remove the blank spaces at the end, but not the ones in the front. How can I make my function modify the string passed to it? Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > trim( a );
    This ignores the return result.
    Try something like
    char *p = trim( a );

    Then print what p points to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Code:
    #include <stdio.h>
    
    int Trim(char* buffer, char* s)
    {
    	int index = 0;
    	int len = 0;
    	for(; s[index] != '\0'; index++)
    	{
    		if (s[index] != ' ')
    		{
    			buffer[len] = s[index];
    			len++;
    		}
    	}
    	buffer[len] = 0;
    	// returns the length of buffer:
    	return len;
    }
    
    int main()
    {
    	char buff[256];
    	char str[] = "|           Hello World   |";
    	Trim(buff, str);
    	printf("Normal string: %s\nSame string, but trimmed: %s\n", str, buff);
    	system("pause"); // Not portable
    	return 0;
    }
    Copy the char array to the buffer only if is not a blank space

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Just a little suggestion Joelito...
    In C it is correct to use int main (void) and not int main() if you don't want main to take arguments For a further information see this.

    Also you can use
    Code:
    buffer[len] = '\0';
    because '\0' is character constant although your way is OK.

    Cheers!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    @Micko: Thanks... I already know that
    But since I'm more C++ than C... sometimes I mix those two

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Is there a way to make my function change the array without the need of another variable?

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Look at Salem's post, his solves your problem. Your function works, you're just not handling the return value correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trim spaces
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-04-2009, 11:24 PM
  2. trim heading and trailing space
    By George2 in forum C Programming
    Replies: 1
    Last Post: 10-20-2007, 11:41 PM
  3. Replies: 9
    Last Post: 04-21-2004, 01:52 PM
  4. Trim A String
    By pittuck in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2003, 07:38 AM
  5. trim string function (code)
    By ipe in forum C Programming
    Replies: 9
    Last Post: 01-06-2003, 12:28 AM