Function name: strncmp
Header file : <string.h>
Function prototype : int strncmp(const char *str1,const char *str2,int n);
Function : Compare two strings with a specified number of strings
Parameters : str1 and str2 are the strings to be compared.
int n is the number of strings to be compared
Return value : str1 > str2 returns a value greater than 0;
str1==str2 returns a value equal to 0;
str1 < str2 returns a value less than 0;
Note: This function returns not a fixed value like 1 or -1, but a value greater than or less than 0
Program example : Compare string s2 with the first n characters of strings s1 and s3, and output the result
#include<stdio.h>#include<string.h>intmain(void){char*s1=www.dotcpp,*s2=dotcpp.com,*s3=dotcpp;intp=strncmp(s2,s1,3); if(p>0){printf(s2isgreaterthans1n);}elseif(p<0){printf(s2islessthans1n);}else{printf(s2isequalss1n);}p=strncmp(s2,s3,3) ;if(p>0){printf(s2isgreaterthans3n);}elseif(p<0){printf(s2islessthans3n);}else{printf(s2isequalss3n);}return0;}
Running results:
s2islessthans1s2isequalss3