strchr strrchr strcmp的实现

strchr的实现

#include <stdio.h>


char *my_strchr(const char *string, char ch)
{
    const char *str = string;
    char *p_char = NULL;
    if(NULL == string){
        return p_char;
    }
    while(*str++ != '\0'){
        if(*str == ch){  
            p_char = (char *)str;
            return p_char;
        }
    }
    return p_char;
}

int main(int argc, char **argv)
{
    char *p = NULL;
    char *str1 = "hello, world";
    char ch = 'l';
    p = my_strchr(str1, ch);
    printf("%p\n", p);
    return 0;

}

strrchr的实现

#include <stdio.h>

char *my_strrchr(const char *string, char ch)
{
    const char *str = string;
    char *p_char = NULL;
    if(NULL == string){
        return p_char;
    }
    while(*str++ != '\0'){
        if(*str == ch){
            p_char = (char *)str;
        }
    }
    return p_char;
}

int main(int argc, char **argv)
{
    char *p = NULL;
    char *str1 = "hello, world";
    char ch = 'l';
    p = my_strrchr(str1, ch);
    printf("%p\n", p);
    return 0;
}

strcmp的实现

#include <stdio.h>
#include <stdlib.h>

void my_strcmp(const char *string1, const char *string2)
{
    const char *str1 = string1;
    const char *str2 = string2;
    int result = 0;
    if(NULL != string1 && NULL != string2){
        while(*str1 && *str2 && *str1 == *str2){
            str1++;
            str2++;
        }
        result = *str1 - *str2;
        printf("%d\n", result);
        if(0 == result){
            printf("string1 = string2\n");
        }
    }
}

int main(int argc, char **argv)
{
    char *str1 = "hello, world";
    char *str2 = "hello, worle";
    my_strcmp(str1, str2);
    return 0;
}

你可能感兴趣的:(strchr strrchr strcmp的实现)