#include "strings.h"
#include "stdint.h"

int int_strncmp(const char* s1, const char* s2, size_t n) {
    while (n > 0) {
        if (*s1 != *s2) {
            return (uint8_t)*s1 - (uint8_t)*s2;
        }
        if (*s1 == '\0') {
            return 0;  // End of both strings
        }
        s1++;
        s2++;
        n--;
    }
    return 0;  // First n characters are equal
}


int strcmp(const char *str1, const char *str2) {
    while (*str1 && (*str1 == *str2)) {
        str1++;
        str2++;
    }
    return *(unsigned char *)str1 - *(unsigned char *)str2;
}




void strcpy(char *dest, const char *src) {
    while (*src) {
        *dest = *src;
        dest++;
        src++;
    }
    *dest = '\0';  // Ensure the destination string is null-terminated
}



size_t strlen(const char *str) {
    const char *s = str;
    while (*s) {
        s++;
    }
    return s - str;
}




char *strncpy(char *dest, const char *src, size_t n) {
    char *d = dest;
    while (n && (*d++ = *src++)) {
        n--;
    }
    if (n) {
        while (--n) {
            *d++ = '\0';
        }
    }
    return dest;
}



char* strrchr(const char* str, char ch) {
    char* result = NULL;
    while (*str) {
        if (*str == ch) {
            result = (char*)str;
        }
        str++;
    }
    return result;
}


// In strings.c
int strncmp(const char *s1, const char *s2, size_t n) {
    while (n && *s1 && (*s1 == *s2)) {
        ++s1;
        ++s2;
        --n;
    }
    if (n == 0) {
        return 0;
    } else {
        return (*(unsigned char *)s1 - *(unsigned char *)s2);
    }
}

char* strchr(const char* str, int ch) {
    while (*str) {
        if (*str == (char)ch) {
            return (char*)str;
        }
        str++;
    }
    return NULL;
}
