Last active
December 18, 2015 21:39
-
-
Save Apkawa/5849370 to your computer and use it in GitHub Desktop.
Revisions
-
Apkawa revised this gist
Sep 11, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -60,7 +60,7 @@ void str_cat(char **s, const char *word) { } size_t old_size = sizeof(char) * strlen(*s); size_t size = sizeof(char) * strlen(word) + 1; if ((*s = realloc(*s, old_size + size)) != NULL) { memcpy(*s + old_size, word, size); -
Apkawa revised this gist
Jun 24, 2013 . 1 changed file with 9 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,10 +40,11 @@ * @param word - строка */ void str_cpy(char **s, const char *word) { size_t size = sizeof(char) * strlen(word) + 1; if ((*s = malloc(size)) != NULL) { memcpy(*s, word, size); } } @@ -58,10 +59,12 @@ void str_cat(char **s, const char *word) { return; } size_t old_size = sizeof(char) * strlen(*s); size_t size = sizeof(char) * strlen(word) + 1; if ((*s = realloc(*s, old_size + size)) != NULL); { memcpy(*s + old_size, word, size); } } @@ -84,7 +87,7 @@ int main(int argc, char *argv[]) puts(s); /* result: "Hola World!" */ str_free(&s); //char hello[] = "hello"; char *hello_ptr = hello; //str_cat(&hello_ptr, " World"); /* oops =C */ return 0; } -
Apkawa revised this gist
Jun 24, 2013 . 1 changed file with 15 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,8 +41,11 @@ */ void str_cpy(char **s, const char *word) { size_t size = sizeof(char) * strlen(word); if ((*s = malloc(size + 1)) != NULL) { memcpy(*s, word, size); } } /** @@ -51,10 +54,15 @@ void str_cpy(char **s, const char *word) { * @param word подстрока */ void str_cat(char **s, const char *word) { if (*s == NULL) { return; } size_t old_size = sizeof(char) * strlen(*s); size_t size = sizeof(char) * strlen(word); if ((*s = realloc(*s, old_size + size + 1)) != NULL); { memcpy(*s + old_size, word, size); } } void str_printf(char **s, const char *format, const char *word) { @@ -75,5 +83,8 @@ int main(int argc, char *argv[]) str_printf(&s, "%s!", s); puts(s); /* result: "Hola World!" */ str_free(&s); char hello[] = "hello"; char *hello_ptr = hello; str_cat(&hello_ptr, " World"); /* oops =C */ return 0; } -
Apkawa revised this gist
Jun 24, 2013 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,15 +34,25 @@ #include <string.h> #include <stdarg.h> /** * @brief str_cpy Функция копирования строки в указатель. Небезопасная, длинна строки определяется по \0 * @param s - указатель * @param word - строка */ void str_cpy(char **s, const char *word) { size_t size = sizeof(char) * strlen(word); *s = malloc(size); memcpy(*s, word, size); } /** * @brief str_cat Функция добавления подстроки к указателю. Небезопасная. * @param s Указатель * @param word подстрока */ void str_cat(char **s, const char *word) { size_t old_size = sizeof(char) * strlen(*s); size_t size = sizeof(char) * strlen(word); *s = realloc(*s, old_size + size); memcpy(*s + old_size, word, size); } @@ -64,7 +74,6 @@ int main(int argc, char *argv[]) str_cat(&s, " World"); str_printf(&s, "%s!", s); puts(s); /* result: "Hola World!" */ str_free(&s); return 0; } -
Apkawa revised this gist
Jun 24, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,7 +51,7 @@ void str_printf(char **s, const char *format, const char *word) { sprintf(*s, format, word); } void str_free(char **s) { free(*s); *s = NULL; } -
Apkawa created this gist
Jun 24, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ /* Copyright (C) Hola 2012, 2013 * * Добро пожаловать на олимпиаду программистов Hola! * * ПРАВИЛА: * - Идеальное решение до 28-May-13 принесет вам 500 долларов США! * - В случае хорошего (но не идеального) решения, мы вышлем замечания, чтобы * помочь вам доработать код. * - Идеальный ответ со второй попытки принесет вам 250 долларов США. * * ИНСТРУКЦИЯ: * - Посмотрите на main(). Там вызываются различные функции. * - От вас требуется имплементировать str_cpy() и str_cat(). * - Функции str_printf() и str_free() имплементитовать не надо. * - Прочтите внимательно main(), чтобы понять прототип и использование str_cpy() и * str_cat(). * - Код, написанный вами, должен быть на уровне "библиотечного кода", подобно тому * как это сделано в хорошей имплементации libc. * - Обратите внимание на 4 заголовочных файла перед функцией main(). Вы можете * использовать функции, описанные в этих файлах, в своей реализации str_cpy() и * str_cat(). * - У вас есть 15 минут для решения задачи. * * Замечание: Функции str_cpy() и str_cat() можно реализовать эффективно не * более чем 7 строк кода на функцию, и менее чем за 5 минут. * * Отправьте решение на [email protected]. Мы проверим его и сообшим * о результате в течение 3 дней. * * Удачи! */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> void str_cpy(char **s, const char *word) { int size = sizeof(char) * strlen(word); *s = malloc(size); memcpy(*s, word, size); } void str_cat(char **s, const char *word) { signed int old_size = sizeof(char) * strlen(*s); signed int size = sizeof(char) * strlen(word); *s = realloc(*s, old_size + size); memcpy(*s + old_size, word, size); } void str_printf(char **s, const char *format, const char *word) { sprintf(*s, format, word); } void str_free(void **s) { free(*s); *s = NULL; } int main(int argc, char *argv[]) { char *s = NULL; str_cpy(&s, "Hola Hola"); str_cpy(&s, s+5); str_cat(&s, " World"); str_printf(&s, "%s!", s); puts(s); /* result: "Hola World!" */ printf("%s\n", s); str_free(&s); return 0; }