C strncmp()-string comparison up to n

<<Previous

Next >>





It is similar to strcmp() but only difference is that it compares the characters up to the specified length.

Syntax:
	int strcmp(const char *s1,const char *s2,size_t number);

It returns zero value if both strings are same or it returns negative value if string s1 is less than s2 or it returns positive value if s1 is greater than s2.

Following is an example program using strncmp()

#include<stdio.h>

#include<string.h>


main()
{
  char first[]={’W’,‘E’,‘L’,‘C’, ‘O’,‘M’,‘E’};
  char second[];
  int result;

  clrscr();
  printf(" The first string is %s ",first);
  printf(" \n Enter the second string ");
  scanf(" %s ",&second);

  result = strncmp(first,second,3 );
  if(result==0)
  {
  	printf(" \n The comparison of two strings are same ");
  }
  else
  {
  	printf(" \n The comparison of two strings are different ");
  }
  return(0);
}

Output:

The first string is WELCOME
Enter the second string WELCOME
The comparison of two strings are same

<< Previous

Next >>







C strncmp()-string comparison up to n

<<Previous

Next >>





It is similar to strcmp() but only difference is that it compares the characters up to the specified length.

Syntax:
	int strcmp(const char *s1,const char *s2,size_t number);

It returns zero value if both strings are same or it returns negative value if string s1 is less than s2 or it returns positive value if s1 is greater than s2.

Following is an example program using strncmp()

#include<stdio.h>

#include<string.h>


main()
{
  char first[]={’W’,‘E’,‘L’,‘C’, ‘O’,‘M’,‘E’};
  char second[];
  int result;

  clrscr();
  printf(" The first string is %s ",first);
  printf(" \n Enter the second string ");
  scanf(" %s ",&second);

  result = strncmp(first,second,3 );
  if(result==0)
  {
  	printf(" \n The comparison of two strings are same ");
  }
  else
  {
  	printf(" \n The comparison of two strings are different ");
  }
  return(0);
}

Output:

The first string is WELCOME
Enter the second string WELCOME
The comparison of two strings are same

<< Previous

Next >>







Learn about Hashing and Hash table

Learn Expression Tree

OS Kernel Architecture

OS Arcitecture of Windows, Linux and MacOS

Learn OS scheduling Algorithms