Table of Contents
show
Use of strncmp( ) function
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50];
char s2[50];
int res,n;
puts("Enter string1:");
gets(s1);
puts("Enter string2:");
gets(s2);
puts("Enter the number of characters to compare");
scanf("%d",&n);
puts("strings are:");
puts(s1);
puts(s2);
res=strncmp(s1,s2,n);
if(res == 0)
{
puts("string portions are equal");
}
else
{
puts("string portions are unequal");
}
return 0;
}
Output
Enter string1:
Hello
Enter string2:
hello
Enter the number of characters to compare
2
strings are:
Hello
hello
string portions are unequal
Views: 0