C-Kurs/Text processing/Musterlösung: Unterschied zwischen den Versionen
< C-Kurs | Text processing
(Die Seite wurde neu angelegt: „ #include <stdio.h> #include <assert.h> #include <string.h> void write_and_advance(char **write, const char *text) { strcpy(*write, text); *writ...“) |
Mutax (Diskussion | Beiträge) K (hat „Ckurs2009/Text processing/Musterlösung“ nach „Ckurs/Text processing/Musterlösung“ verschoben: Aufgaben sind Kurs übergreifend nutzbar) |
(kein Unterschied)
|
Version vom 25. Juli 2010, 11:49 Uhr
#include <stdio.h> #include <assert.h> #include <string.h> void write_and_advance(char **write, const char *text) { strcpy(*write, text); *write += strlen(text); } int copy_escape_xml(char *output, const char *input) { const char *read = input; char *write = output; assert(input != NULL); assert(output != NULL); for (;;) { switch (read[0]) { case '\0': write[0] = '\0'; return write - output; /* == strlen(output) */ case '&': write_and_advance(&write, "&"); break; case '<': write_and_advance(&write, "<"); break; case '>': write_and_advance(&write, ">"); break; case '\'': write_and_advance(&write, "'"); break; case '"': write_and_advance(&write, """); break; default: *(write++) = read[0]; } read++; } } void single_slashes(char *input_output) { const char *read = input_output; char *write = input_output; assert(input_output != NULL); for (;;) { switch (read[0]) { case '\0': write[0] = '\0'; return; case '/': { /* Advance after last slash in sequence */ read++; while (read[0] == '/') { read++; } write[0] = '/'; write++; break; } default: *(write++) = read[0]; read++; } } } int main() { char escape_input[] = "Kernighan & Ritchie"; char escape_output[255]; char slash_input_output[] = "/usr//bin///env"; copy_escape_xml(escape_output, escape_input); single_slashes(slash_input_output); printf("copy_escape_xml --> \"%s\"\n", escape_output); printf("single_slashes --> \"%s\"\n", slash_input_output); return 0; }