#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n);
#include <stdio.h>
#include <string.h>
int main()
{
char s1[100];
const char *s2 = "hello world";
int len;
printf ("input a string:");
fgets (s1, 100, stdin);
len = strncmp (s1, s2, 5);
if (!len) {
printf ("Full match!\n");
return (0);
}
if ( len > 0) {
printf ("s1 > s2\n");
return (1);
}
printf ("s1 < s2\n");
return (2);
}
# ./strncmp
input a string:hello
Full match!
# ./strncmp
input a string:hellow
Full match!
# ./strncmp
input a string:hellp
s1 > s2
# ./strncmp
input a string:hez
s1 > s2
# ./strncmp
input a string:z
s1 > s2
# ./strncmp
input a string:he
s1 < s2
# ./strncmp
input a string:helln
s1 < s2
# ./strncmp
input a string:a
s1 < s2
/**
* strncmp - Compare two length-limited strings
* @cs: One string
* @ct: Another string
* @count: The maximum number of bytes to compare
*/
int strncmp(const char * cs,const char * ct,size_t count)
{
register signed char __res = 0;
while (count) {
if ((__res = *cs - *ct++) != 0 || !*cs++)
break;
count--;
}
return __res;
}
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |