/
solidbase
/
C3_s21_stringplus
Обзор
Документация
Войти
/
solidbase
/
C3_s21_stringplus
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/s21_mem.c
57 строк
1 KB
Dmitrii Isaev
Implement s21_memset
04 янв 2026, 18:39
04 янв 2026, 18:39
70eac68
Код
Авторство
О чём код?
#include "s21_string.h" // 1. Searches for the first occurrence of the character c (an unsigned char) in // the first n bytes of the string pointed to, by the argument str. void* s21_memchr(const void* str, int c, s21_size n) { const unsigned char* ptr = (const unsigned char*)str; unsigned char target = (unsigned char)c; void* result = S21_NULL; for (s21_size i = 0; i < n && result == S21_NULL; i++) { if (ptr[i] == target) { result = (void*)(ptr + i); } } return result; } // 2. Compares the first n bytes of str1 and str2. int s21_memcmp(const void* str1, const void* str2, s21_size n) { const unsigned char* ptr1 = (const unsigned char*)str1; const unsigned char* ptr2 = (const unsigned char*)str2; int result = 0; for (s21_size i = 0; i < n && result == 0; i++) { if (ptr1[i] != ptr2[i]) { result = ptr1[i] - ptr2[i]; } } return result; } // 3. Copies n characters from src to dest. void* s21_memcpy(void* dest, const void* src, s21_size n) { unsigned char* dest_ptr = (unsigned char*)dest; const unsigned char* src_ptr = (const unsigned char*)src; for (s21_size i = 0; i < n; i++) { dest_ptr[i] = src_ptr[i]; } return dest; } // 4. Copies the character c to the first n characters of the string pointed to // by str. void* s21_memset(void* str, int c, s21_size n) { unsigned char* ptr = (unsigned char*)str; unsigned char value = (unsigned char)c; for (s21_size i = 0; i < n; i++) { ptr[i] = value; } return str; }