Skip to content

Instantly share code, notes, and snippets.

@Apkawa
Last active December 18, 2015 21:39
Show Gist options
  • Select an option

  • Save Apkawa/5849370 to your computer and use it in GitHub Desktop.

Select an option

Save Apkawa/5849370 to your computer and use it in GitHub Desktop.

Revisions

  1. Apkawa revised this gist Sep 11, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hola.c
    Original 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);
    if ((*s = realloc(*s, old_size + size)) != NULL)
    {
    memcpy(*s + old_size, word, size);

  2. Apkawa revised this gist Jun 24, 2013. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions hola.c
    Original 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);
    if ((*s = malloc(size + 1)) != NULL)
    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);
    if ((*s = realloc(*s, old_size + size + 1)) != NULL);
    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 */
    //char hello[] = "hello"; char *hello_ptr = hello;
    //str_cat(&hello_ptr, " World"); /* oops =C */
    return 0;
    }
  3. Apkawa revised this gist Jun 24, 2013. 1 changed file with 15 additions and 4 deletions.
    19 changes: 15 additions & 4 deletions hola.c
    Original 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);
    *s = malloc(size);
    memcpy(*s, word, size);
    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);
    *s = realloc(*s, old_size + size);
    memcpy(*s + old_size, word, size);
    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;
    }
  4. Apkawa revised this gist Jun 24, 2013. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions hola.c
    Original 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) {
    int size = sizeof(char) * strlen(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) {
    signed int old_size = sizeof(char) * strlen(*s);
    signed int size = sizeof(char) * strlen(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!" */
    printf("%s\n", s);
    str_free(&s);
    return 0;
    }
    }
  5. Apkawa revised this gist Jun 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hola.c
    Original 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(void **s) {
    void str_free(char **s) {
    free(*s);
    *s = NULL;
    }
  6. Apkawa created this gist Jun 24, 2013.
    70 changes: 70 additions & 0 deletions hola.c
    Original 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;
    }