Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include "s21_string.h"
- void input(char *a);
- void output(char *a);
- /*
- int main() {
- char *str1 = "1235";
- char *str2 = "1234";
- printf("Длина\n");
- s21_size_t l1 = s21_strlen(str1);
- printf("len = %d\n\n", l1);
- //input(str1);
- //input(str2);
- printf("сравнение\n");
- s21_size_t f = s21_strcmp(str1, str2);
- printf("flag = %d\n", f);
- printf("original flag = %d\n\n", strcmp(str1, str2));
- printf("сравнение n\n");
- s21_size_t g = s21_strncmp(str1, str2, 4);
- printf("flag = %d\n", g);
- printf("original flag = %d\n\n", strncmp(str1, str2, 4));
- printf("копия\n");
- char buf[6]="";
- s21_strcpy(buf, str1);
- printf("%s\n\n", buf);
- printf("копия n\n");
- char buf1[32]="";
- s21_strncpy(buf1, str1, 8);
- printf("%s\n\n", buf1);
- output(str1);
- output(str2);
- return 0;
- }
- */
- void input(char *a) {
- for (int i = 0; i < 10; i++) {
- scanf("%c ", a+i);
- }
- }
- void output(char *a) {
- int len = s21_strlen(a);
- for (int p = 0; p < len; p++) {
- printf("%c", *(a+p));
- }
- printf("\n");
- }
- int s21_strcmp(const char *str1, const char *str2) {
- int f = 0;
- int len = s21_strlen(str1);
- int len2 = s21_strlen(str2);
- if(len2 > len)
- len = len2;
- for (int i = 0; i < len; ++i) {
- if (*(str1+i) > *(str2+i)) {
- f = *(str1+i) - *(str2+i);
- break;
- } else if (*(str1+i) < *(str2+i)){
- // f = -1;
- f = *(str1+i) - *(str2+i);
- break;
- }
- }
- return f;
- }
- s21_size_t s21_strlen(const char *str)
- {
- s21_size_t len = 0;
- while (1) {
- if (*(str+len) == '\0')
- break;
- len++;
- }
- return len;
- }
- s21_size_t s21_strncmp(const char *str1, const char *str2, s21_size_t n) {
- int f = 0;
- for (int i = 0; i < n; ++i) {
- if (*(str1+i) > *(str2+i)) {
- f = *(str1+i) - *(str2+i);
- break;
- } else if (*(str1+i) < *(str2+i)){
- f = *(str1+i) - *(str2+i);
- break;
- }
- }
- return f;
- }
- char *s21_strcpy(char *dest, const char *src) {
- int i = 0;
- while (*(src+i) != '\0') {
- *(dest+i) = *(src+i);
- i++;
- }
- return dest;
- }
- char *s21_strncpy(char *dest, const char *src, int n) {
- for (int i = 0; i < n; i++) {
- *(dest+i) = *(src+i);
- }
- return dest;
- }
- /*
- int s21_memcmp(const void *str1, const void *str2, s21_size_t n) {
- int returnable = 0;
- char *str1_temp = (char *)str1;
- char *str2_temp = (char *)str2;
- if (str1_temp == NULL && str2_temp == NULL) {
- returnable = 0;
- } else if (str1_temp == NULL && str2_temp != NULL) {
- returnable = 1;
- } else if (str1_temp != NULL && str2_temp == NULL) {
- returnable = -1;
- } else {
- s21_size_t i = 0;
- while (i < n && str1_temp[i] == str2_temp[i]) {
- i++;
- }
- if (i != n && str1_temp[i] > str2_temp[i]) {
- returnable = 1;
- } else if (i != n && str1_temp[i] < str2_temp[i]) {
- returnable = -1;
- } else {
- returnable = 0;
- }
- }
- return returnable;
- }
- */
Add Comment
Please, Sign In to add comment