Загрузка данных
#ifndef S21_STRING_H
#define S21_STRING_H
#include <stdlib.h>
size_t s21_strlen(const char *str);
int s21_strcmp(const char *str1, const char *str2);
char *s21_strcpy(char *dest, const char *src);
char *s21_strcat(char *dest, const char *src);
char *s21_strchr(const char *str, int c);
char *s21_strstr(const char *haystack, const char *needle);
#endif
#include "s21_string.h"
size_t s21_strlen(const char *str) {
size_t length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int s21_strcmp(const char *str1, const char *str2) {
int i = 0;
while (str1[i] != '\0' && str1[i] == str2[i]) {
i++;
}
return (unsigned char)str1[i] - (unsigned char)str2[i];
}
char *s21_strcpy(char *dest, const char *src) {
int i = 0;
while (src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return dest;
}
char *s21_strcat(char *dest, const char *src) {
int dest_len = s21_strlen(dest);
int i = 0;
while (src[i] != '\0') {
dest[dest_len + i] = src[i];
i++;
}
dest[dest_len + i] = '\0';
return dest;
}
char *s21_strchr(const char *str, int c) {
char *result = NULL;
int i = 0;
while (str[i] != '\0' && result == NULL) {
if (str[i] == (char)c) {
result = (char *)(str + i);
}
i++;
}
if ((char)c == '\0') {
result = (char *)(str + i);
}
return result;
}
char *s21_strstr(const char *haystack, const char *needle) {
char *result = NULL;
if (*needle == '\0') {
result = (char *)haystack;
}
for (int i = 0; haystack[i] != '\0' && result == NULL; i++) {
int j = 0;
while (needle[j] != '\0' && haystack[i + j] == needle[j]) {
j++;
}
if (needle[j] == '\0') {
result = (char *)(haystack + i);
}
}
return result;
}
#include <stdio.h>
#include <stdlib.h>
#include "s21_string.h"
void s21_strlen_test(void);
void s21_strcmp_test(void);
void s21_strcpy_test(void);
void s21_strcat_test(void);
void s21_strchr_test(void);
void s21_strstr_test(void);
void print_strlen_result(const char *input, size_t result, size_t expected);
void print_strcmp_result(const char *s1, const char *s2, int result, int expected);
void print_string_result(const char *test_name, const char *input, const char *result,
const char *expected);
void print_pointer_result(const char *test_name, const char *input, char c, const char *result,
const char *expected);
int main(void) {
s21_strlen_test();
s21_strcmp_test();
s21_strcpy_test();
s21_strcat_test();
s21_strchr_test();
s21_strstr_test();
return 0;
}
void s21_strlen_test(void) {
print_strlen_result("hello", s21_strlen("hello"), 5);
print_strlen_result("", s21_strlen(""), 0);
print_strlen_result("21 school", s21_strlen("21 school"), 9);
}
void s21_strcmp_test(void) {
print_strcmp_result("abc", "abc", s21_strcmp("abc", "abc"), 0);
print_strcmp_result("abc", "abd", s21_strcmp("abc", "abd"), -1);
print_strcmp_result("abd", "abc", s21_strcmp("abd", "abc"), 1);
}
void s21_strcpy_test(void) {
char dest1[32];
char dest2[32];
char dest3[32];
print_string_result("s21_strcpy_test", "hello", s21_strcpy(dest1, "hello"), "hello");
print_string_result("s21_strcpy_test", "", s21_strcpy(dest2, ""), "");
print_string_result("s21_strcpy_test", "21 school", s21_strcpy(dest3, "21 school"),
"21 school");
}
void s21_strcat_test(void) {
char dest1[32] = "hello";
char dest2[32] = "";
char dest3[32] = "21";
print_string_result("s21_strcat_test", "hello + world", s21_strcat(dest1, " world"),
"hello world");
print_string_result("s21_strcat_test", "empty + abc", s21_strcat(dest2, "abc"), "abc");
print_string_result("s21_strcat_test", "21 + school", s21_strcat(dest3, " school"),
"21 school");
}
void s21_strchr_test(void) {
print_pointer_result("s21_strchr_test", "hello", 'e', s21_strchr("hello", 'e'), "ello");
print_pointer_result("s21_strchr_test", "hello", 'z', s21_strchr("hello", 'z'), NULL);
print_pointer_result("s21_strchr_test", "hello", '\0', s21_strchr("hello", '\0'), "");
}
void s21_strstr_test(void) {
print_string_result("s21_strstr_test", "hello/ll", s21_strstr("hello", "ll"), "llo");
print_string_result("s21_strstr_test", "hello/empty", s21_strstr("hello", ""), "hello");
print_string_result("s21_strstr_test", "hello/zz", s21_strstr("hello", "zz"), NULL);
}
void print_strlen_result(const char *input, size_t result, size_t expected) {
printf("s21_strlen_test input: \"%s\" output: %lu result: ", input, result);
if (result == expected) {
printf("SUCCESS\n");
} else {
printf("FAIL\n");
}
}
void print_strcmp_result(const char *s1, const char *s2, int result, int expected) {
printf("s21_strcmp_test input: \"%s\" \"%s\" output: %d result: ", s1, s2, result);
if ((result == 0 && expected == 0) || (result < 0 && expected < 0) ||
(result > 0 && expected > 0)) {
printf("SUCCESS\n");
} else {
printf("FAIL\n");
}
}
void print_string_result(const char *test_name, const char *input, const char *result,
const char *expected) {
printf("%s input: \"%s\" output: ", test_name, input);
if (result == NULL) {
printf("NULL");
} else {
printf("\"%s\"", result);
}
printf(" result: ");
if ((result == NULL && expected == NULL) ||
(result != NULL && expected != NULL && s21_strcmp(result, expected) == 0)) {
printf("SUCCESS\n");
} else {
printf("FAIL\n");
}
}
void print_pointer_result(const char *test_name, const char *input, char c, const char *result,
const char *expected) {
printf("%s input: \"%s\" '%c' output: ", test_name, input, c);
if (result == NULL) {
printf("NULL");
} else {
printf("\"%s\"", result);
}
printf(" result: ");
if ((result == NULL && expected == NULL) ||
(result != NULL && expected != NULL && s21_strcmp(result, expected) == 0)) {
printf("SUCCESS\n");
} else {
printf("FAIL\n");
}
}
CC = gcc
CFLAGS = -Wall -Wextra -Werror -std=c11
BUILD_DIR = ../../build
strlen_tests:
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) s21_string.c s21_string_test.c -o $(BUILD_DIR)/Quest_6
strcmp_tests:
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) s21_string.c s21_string_test.c -o $(BUILD_DIR)/Quest_7
strcpy:
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) s21_string.c s21_string_test.c -o $(BUILD_DIR)/Quest_8
strcat:
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) s21_string.c s21_string_test.c -o $(BUILD_DIR)/Quest_9
strchr:
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) s21_string.c s21_string_test.c -o $(BUILD_DIR)/Quest_10
strstr:
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) s21_string.c s21_string_test.c -o $(BUILD_DIR)/Quest_11
full_coverage_tests:
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) s21_string.c s21_string_test.c -o $(BUILD_DIR)/Quest_12
clean:
rm -rf $(BUILD_DIR)
rebuild: clean strlen_tests strcmp_tests strcpy strcat strchr strstr full_coverage_tests