Trim

This is a discussion on Trim within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> char *trim( char *array ) { int i; for( ; *array == ' '; array++ ); i ...

  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
    32,825
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    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
    712
    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
    308
    @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
    University of Waterloo
    Posts
    1,904
    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-05-2009, 12:24 AM
  2. trim heading and trailing space
    By George2 in forum C Programming
    Replies: 1
    Last Post: 10-21-2007, 12:41 AM
  3. Replies: 9
    Last Post: 04-21-2004, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21