String parsing in C. Copying a part of a string to another string -
i'm trying write function convert escape sequences in string in non-printable form. if have string "this \n makes new line", "this makes new line". far i've got this. i'm calling main:
int main() { unescape("this \\n\\n \\t\\t\\t string number \\t 7."); return 0; } char* unescape(char* s) { char *esc[2] = {"\\n", "\\t"}; int i; char* uus = (char*)calloc(80, sizeof(char)); char* uus2 = (char*)calloc(80,sizeof(char)); strncpy(uus, s, strlen(s)); for(i = 0; < 2; i++) { while(strstr(uus, esc[i]) != null) //checks if \\n can found { //printf("\n\n%p\n\n", strstr(uus, esc[i])); int c = strstr(uus, esc[i]) - uus; //gets difference between address of beginning of string , location //where searchable string found uus2 = strncpy(uus2, uus, c); //copies beginning of string new string //add check esc being used strcat(uus2, "\n"); //adds non-printable form of escape sequence printf("%s", uus2); //should clear string uus before writing uus2 strncpy(uus, uus2, strlen(uus2)); //copies string uus2 uus can checked again } } //this should return in end. }
basically, need now, take part string uus after "\n" , add string uus2 can run while loop again. thought using strtok hit wall makes 2 separate strings using kind of delimiter not there in case.
edit: adding rest of string uus2 should before strncpy. code without it.
edit vol2: code works , ended using. edited ruud's version bit function had use had return string. lot.
char* unescape(char* s) { char *uus = (char*) calloc(80, sizeof(char)); int = 0; while (*s != '\0') { char c = *s++; if (c == '\\' && *s != '\0') { c = *s++; switch (c) { case 'n': c = '\n'; break; case 't': c = '\t'; break; } } uus[i] = c; i++; } uus[i] = '\0'; return uus; }
i agree anonymouse. both clumsy , inefficient replace first \n
, \t
. instead, make single pass through string, replacing escape characters go.
i left space allocation out in code sample below; imho separate responsibility, not part of algorithm, , such not belong in same function.
void unescape(char *target, const char *source) { while (*source != '\0') { char c = *source++; if (c == '\\' && *source != '\0') { c = *source++; switch (c) { case 'n': c = '\n'; break; case 't': c = '\t'; break; } } *target++ = c; } *target = '\0'; }
edit: here's alternative version, using strchr
suggested anonymouse. implementation should faster, on long strings relatively few escape characters. posted demonstration of how optimizations can make code more complex , less readable; , consequently less maintainable , more error-prone. detailed discussion, see: http://c2.com/cgi/wiki?optimizelater
void unescape(char *target, const char *source) { while (*source != '\0') { if (*source++ == '\\' && *source != '\0') { char c = *source++; switch (c) { case 'n': c = '\n'; break; case 't': c = '\t'; break; } *target++ = c; } else { const char *escape = strchr(source--, '\\'); int numberofchars = escape != null ? escape - source : strlen(source); strncpy(target, source, numberofchars); target += numberofchars; source += numberofchars; } } *target = '\0'; }
Comments
Post a Comment