Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Functions and Pointers
  5. Built-in functions
  6. Compare n Chars

Compare n Chars

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

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments