Загрузка данных
Отлично, Quest 7 готов.
По скринам видно:
make strcmp_tests
собрал:
../../build/Quest_7
Тесты strlen и strcmp проходят:
Result: SUCCESS
Проверка памяти тоже прошла:
All heap blocks were freed -- no leaks are possible
ERROR SUMMARY: 0 errors
Значит сейчас:
Quest 6 — s21_strlen — OK
Quest 7 — s21_strcmp — OK
Переходим к Quest 8 — s21_strcpy.
⸻
Quest 8 — s21_strcpy
Нужно добавить функцию:
char *s21_strcpy(char *destination, const char *source);
Она копирует строку source в destination, включая последний символ '\0'.
⸻
Шаг 1. Открой s21_string.h
Ты сейчас в папке:
src/s21_string
Открой файл:
nano s21_string.h
Полностью замени содержимое на это:
#ifndef S21_STRING_H
#define S21_STRING_H
#include <stddef.h>
size_t s21_strlen(const char *str);
int s21_strcmp(const char *str1, const char *str2);
char *s21_strcpy(char *destination, const char *source);
#endif
Сохрани.
⸻
Шаг 2. Открой s21_string.c
nano s21_string.c
Полностью замени содержимое на это:
#include "s21_string.h"
size_t s21_strlen(const char *str) {
size_t length = 0;
if (str != NULL) {
while (str[length] != '\0') {
length++;
}
}
return length;
}
int s21_strcmp(const char *str1, const char *str2) {
int result = 0;
int i = 0;
while (result == 0 && str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
result = str1[i] - str2[i];
}
i++;
}
if (result == 0) {
result = str1[i] - str2[i];
}
return result;
}
char *s21_strcpy(char *destination, const char *source) {
int i = 0;
while (source[i] != '\0') {
destination[i] = source[i];
i++;
}
destination[i] = '\0';
return destination;
}
Сохрани.
⸻
Шаг 3. Открой s21_string_test.c
nano s21_string_test.c
Полностью замени содержимое на это:
#include <stdio.h>
#include "s21_string.h"
void s21_strlen_test(void);
void s21_strcmp_test(void);
void s21_strcpy_test(void);
void run_strlen_test(const char *input, size_t expected);
void run_strcmp_test(const char *str1, const char *str2, int expected_sign);
void run_strcpy_test(const char *source, const char *expected);
int main(void) {
s21_strlen_test();
s21_strcmp_test();
s21_strcpy_test();
return 0;
}
void s21_strlen_test(void) {
run_strlen_test("hello", 5);
run_strlen_test("", 0);
run_strlen_test("School 21", 9);
run_strlen_test("a", 1);
run_strlen_test("12345", 5);
}
void s21_strcmp_test(void) {
run_strcmp_test("abc", "abc", 0);
run_strcmp_test("abc", "abd", -1);
run_strcmp_test("abd", "abc", 1);
run_strcmp_test("", "", 0);
run_strcmp_test("abc", "abcd", -1);
run_strcmp_test("abcd", "abc", 1);
}
void s21_strcpy_test(void) {
run_strcpy_test("hello", "hello");
run_strcpy_test("", "");
run_strcpy_test("School 21", "School 21");
run_strcpy_test("a", "a");
run_strcpy_test("12345", "12345");
}
void run_strlen_test(const char *input, size_t expected) {
size_t result = s21_strlen(input);
printf("strlen Input: \"%s\"\n", input);
printf("strlen Output: %zu\n", result);
if (result == expected) {
printf("strlen Result: SUCCESS\n");
} else {
printf("strlen Result: FAIL\n");
}
}
void run_strcmp_test(const char *str1, const char *str2, int expected_sign) {
int result = s21_strcmp(str1, str2);
int success = 0;
if (expected_sign == 0 && result == 0) {
success = 1;
} else if (expected_sign < 0 && result < 0) {
success = 1;
} else if (expected_sign > 0 && result > 0) {
success = 1;
}
printf("strcmp Input: \"%s\" \"%s\"\n", str1, str2);
printf("strcmp Output: %d\n", result);
if (success) {
printf("strcmp Result: SUCCESS\n");
} else {
printf("strcmp Result: FAIL\n");
}
}
void run_strcpy_test(const char *source, const char *expected) {
char destination[100];
s21_strcpy(destination, source);
printf("strcpy Input: \"%s\"\n", source);
printf("strcpy Output: \"%s\"\n", destination);
if (s21_strcmp(destination, expected) == 0) {
printf("strcpy Result: SUCCESS\n");
} else {
printf("strcpy Result: FAIL\n");
}
}
Сохрани.
⸻
Шаг 4. Обнови Makefile
nano Makefile
Полностью замени содержимое на это:
CC = gcc
CFLAGS = -Wall -Wextra -Werror
BUILD_DIR = ../../build
SOURCES = s21_string.c s21_string_test.c
strlen_tests: $(BUILD_DIR)/Quest_6
strcmp_tests: $(BUILD_DIR)/Quest_7
strcpy: $(BUILD_DIR)/Quest_8
$(BUILD_DIR)/Quest_6: $(SOURCES) s21_string.h
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $(SOURCES) -o $(BUILD_DIR)/Quest_6
$(BUILD_DIR)/Quest_7: $(SOURCES) s21_string.h
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $(SOURCES) -o $(BUILD_DIR)/Quest_7
$(BUILD_DIR)/Quest_8: $(SOURCES) s21_string.h
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $(SOURCES) -o $(BUILD_DIR)/Quest_8
clean:
rm -rf $(BUILD_DIR)
rebuild: clean strlen_tests strcmp_tests strcpy
Сохрани.
⸻
Шаг 5. Форматирование
clang-format -i *.c *.h
clang-format -n *.c *.h
Если вторая команда ничего не вывела — стиль нормальный.
⸻
Шаг 6. Сборка Quest 8
make strcpy
⸻
Шаг 7. Запуск Quest 8
../../build/Quest_8
Во всех тестах должно быть:
Result: SUCCESS
⸻
Шаг 8. Проверка памяти
valgrind --tool=memcheck --leak-check=yes ../../build/Quest_8
В конце должно быть:
All heap blocks were freed -- no leaks are possible
ERROR SUMMARY: 0 errors
Скинь вывод после ../../build/Quest_8 и valgrind, и пойдём к Quest 9 — s21_strcat.