Thread: Critique my trim function

  1. #1
    Registered User
    Join Date
    Mar 2012

    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.

    #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";
    Last edited by atac; 11-16-2013 at 11:22 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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:
    string[strcspn(string, "\n")] = '\0';
    For visually checking the output, I suggest that you do something like:
    printf("'%s'\n", string);
    Last edited by laserlight; 11-16-2013 at 11:37 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    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
    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
    * 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-06-2003, 12:28 AM