Critique my trim function

This is a discussion on Critique my trim function within the C Programming forums, part of the General Programming Boards category; Just out of the blue I decided to write a trim function, it took me a little while but eventually ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    32

    Critique my trim function

    Just out of the blue I decided to write a trim function, it took me a little while but eventually got it. Can anyone offer some tips on how to optimize it if possible. I feel like theres gotta be another way to offset the string and also do something differently than using that trimmed variable. Thanks.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void trim(char *string){
        int idx, trimmed = 0;
        for(idx = 0; idx < strlen(string); idx++){
            if(string[idx] != ' ' || (string[idx] == ' ' && trimmed)){
                string[trimmed++] = string[idx];    
            }
        }
        const int terminate = strlen(string) - (strlen(string) - trimmed);
        string[terminate] = '\0';
    }
    
    
    int main(){
        char string[] = "   trim this string";
        trim(string);
        puts(string);
    }
    Last edited by atac; 11-16-2013 at 10:22 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    I note that you declared strIdx but did not use it. Since the length of the string does not change, you should just call strlen once and save its return value.

    It might be better to use two loops: one to find the index of the first non-space character; another to copy if the index of the first non-space character is not 0. This way, you can just copy the null character too, and in fact you wouldn't need to call strlen at all.

    By the way, I have seen this called a "left trim" in that you don't also trim trailing spaces.

    Also, fgets will not store the newline into the string if the string read is at the maximum. Therefore, your strchr call is not sufficient for correctness. You should either check that the pointer returned is not a null pointer, or use an alternative such as:
    Code:
    string[strcspn(string, "\n")] = '\0';
    EDIT:
    For visually checking the output, I suggest that you do something like:
    Code:
    printf("'%s'\n", string);
    Last edited by laserlight; 11-16-2013 at 10:37 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    I don't see strchr being used??? Has the code changed?

    Apart from that I'd use isspace() (from ctype.h) to check for whitespace rather than comparing to ' '

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    Thanks, I accidentally included the code for a user to input the string, but removed it because I wanted to get feedback on the logic of the function instead of the technicals of C.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    * Variables 'terminate', 'idx' and 'trimmed' should be type size_t, not int.

    * You should store the value of strlen(string) in another size_t variable and use that variable in your loop and the other 2 places you use strlen()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] trim function serious implementation
    By triceps in forum C Programming
    Replies: 10
    Last Post: 09-12-2011, 04:18 PM
  2. trim function - could you explain please
    By c_lady in forum C Programming
    Replies: 17
    Last Post: 03-27-2010, 09:19 AM
  3. Question about my trim function and pointers
    By space-oddity in forum C Programming
    Replies: 10
    Last Post: 04-28-2008, 01:22 AM
  4. Replies: 9
    Last Post: 04-21-2004, 01:52 PM
  5. trim string function (code)
    By ipe in forum C Programming
    Replies: 9
    Last Post: 01-05-2003, 11:28 PM

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