Thread: [C] trim function serious implementation

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    [C] trim function serious implementation

    Hi everyone, I'm new. I have a request. I'm looking for a C implementation of trim function. I need one that allocates dinamically a string and return this. It mustn't modify the input string. Thanks everyone. Sorry for my English.

    Ivan

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    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
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    I wrote a simple trim function but I have to use this with strtok and this causes bug. I'm not a C programmer so I don't know write it a serious implementation.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by triceps
    I wrote a simple trim function but I have to use this with strtok and this causes bug. I'm not a C programmer so I don't know write it a serious implementation.
    Unfortunately, I have no way of telling for sure if you are say, a physicist who has to write a program in C for something that is secondary to your actual work, or if you are a computing student who is looking for a free lunch. As such, on principle I have to treat this as a homework request. If you are in the former category, then you might want to consider hiring a professional programmer skilled in C to write the program for you.

    So, what algorithm do you have in mind to implement this?
    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

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Hey, I'm honest. I have to develop this project for the university but this trim implementation is not a free lunch. Nobody cares if I don't know how to write this. The point is that I have to know how alghoritms and data structures work. I asked for a serious trim implementation also to my teacher but she haven't replied yet. Anyway, if this is forbidden I'm sorry, you can close the thread.
    Last edited by triceps; 09-12-2011 at 01:41 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by triceps
    The point is that I have to know how alghoritms and data structures work.
    Ah, but then this is a golden opportunity for you to figure out an algorithm on your own. We can point out possible problems with the algorithm that you devise, and then help you fix your implementation if you don't get it right, but you must make the effort to come up with something yourself.
    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

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Well, I tried to implement it but we used Java for the programming course. So I am not a skilled C programmer. I might change my solution.
    This is my function with the bug

    Code:
    Publication get_publication(char* command)
    {
       char *authorsStr, *idsStr;
       Publication p = malloc(sizeof(Publication));
       List authors = NULL, ids = NULL;
       
       if(p == NULL)
       {
          printf("Allocation error\n");
          exit(-1);
       }
    
       command += 2;
    
       p->position = position_from_str_to_int(trim(strtok(command, "|")));
       p->id = trim(strtok(NULL, "|"));
       p->title = trim(strtok(NULL, "|"));
       
       authorsStr = strtok(NULL, "|");   
       idsStr = strtok(NULL, "|");
       printf("%s\n", authorsStr);
       printf("%s\n", idsStr);
       authorsStr = strtok(authorsStr, "&");
    
       while(authorsStr != NULL)
       {
          authors = add_to_list(authors, trim(authorsStr));
          authorsStr = strtok(NULL, "&");
       }   
       
       p->authors = authors;
    
       idsStr = strtok(idsStr, "&");
       
       while(idsStr != NULL)
       {
          ids = add_to_list(ids, trim(idsStr));
          idsStr = strtok(NULL, "&");
       }   
    
       p->bibliography = ids;
    
       printf("%s\n", p->authors->value);
       printf("%s\n", p->authors->next->value);
       printf("%s\n", p->bibliography->value);
       printf("%s\n", p->bibliography->next->value);
    
       return p;
    }
    I expect this output
    Code:
    Zazza
    Mario Rossi
    Pluto
    Giaco mino
    But I obtain
    Code:
    Zazza
    QPQ�
    Pluto
    Giaco mino
    Any advice to change this function without modidy trim function?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > It mustn't modify the input string.
    Then you need to find something other than strtok() then, because this DOES modify the input string.

    > Publication get_publication(char* command)
    Can you just post a function called trim(), without confusing things with how you want to use trim().

    Code:
    int main ( ) {
        char *p = trim("   This    is my         test  string  " );
        printf("Trimmed string=%s\n", p );
        free( p );
        return 0;
    }
    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.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > It mustn't modify the input string.
    Then you need to find something other than strtok() then, because this DOES modify the input string.
    He could just make a copy of the input string with something like strdup(), then he can mangle the copy all he wants.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If only there were some kind of box, say maybe located in the upper right hand corner, that one could type in words, or even a word, and have it spit out some results related to what was typed in. That would be sweet man. I can't wait until technology catches up.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    If only there were some kind of box, say maybe located in the upper right hand corner, that one could type in words, or even a word, and have it spit out some results related to what was typed in. That would be sweet man. I can't wait until technology catches up.
    Quzah.
    Here... Let me google that for you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - gets() function implementation help
    By prakakat in forum C Programming
    Replies: 4
    Last Post: 04-10-2010, 12:09 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