Copy string by length, like strncmp - C String

C examples for String:String Function

Description

Copy string by length, like strncmp

Demo Code

#include <stdio.h>

int _strncmp(char *s, char *t, int n);

int main(void)
{
    char *s, *t;//from   w  w w  . j a v  a  2  s . c  o m

    s = "testabc";
    t = "testddc";

    printf("%s cmp %s: %d\n", s, t, _strncmp(s, t, 5));

    return 0;
}

int _strncmp(char *s, char *t, int n)
{
    for (; n-- > 0 && *s == *t; ++s, ++t){
        if (*s == '\0' || n == 0)
            return 0;
    }        
    return *s - *t;
}

Result


Related Tutorials