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
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
What have you tried?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by triceps
So, what algorithm do you have in mind to implement this?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.Originally Posted by triceps
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
I expect this outputCode: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; }
But I obtainCode:Zazza Mario Rossi Pluto Giaco mino
Any advice to change this function without modidy trim function?Code:Zazza QPQ� Pluto Giaco mino
> 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.
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.
Here... Let me google that for you