Like Tree1Likes
  • 1 Post By laserlight

[C] trim function serious implementation

This is a discussion on [C] trim function serious implementation within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm new. I have a request. I'm looking for a C implementation of trim function. I need one ...

  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
    21,749
    What have you tried?
    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
    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
    21,749
    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?
    AndrewHunter likes this.
    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

  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
    21,749
    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.
    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

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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-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