strncmp() function in string Data structure by R4R Team

-strncmp() is a string function that are able to compare two string upto nth index.
-It is predefined in string.h header file.

Syntax-
strncmp(str1,str2,n)
-Here str1 and str2 is the two string which we compare.
-n is the index value.

program-

#include< stdio.h>
#include< string.h>
int main()
{
char s1[20],s2[20];
//input of string
printf("Enter first string\n");
scanf("%s",&s1);
printf("Enter second string\n");
scanf("%s",&s2);
if(strncmp(s1,s2,4))
printf("Not same");
else
printf("Same");
}


output-

Enter first string
Mystring
Enter second string
Mystring
Same

Enter first string
Mystring
Enter second string
Mys
Not same

Enter first string
Mystring
Enter second string
Myst
Same

-In this program, we take an input of the two string.
-then we ask for the index value.
-with that index value, we pass all data in strncmp() function that compare both string upto nth term.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!