Shares
facebook sharing button Share
twitter sharing button Tweet
email sharing button Email
linkedin sharing button Share
reddit sharing button Share
tumblr sharing button Share
blogger sharing button Share
print sharing button Print
skype sharing button Share
sms sharing button Share
whatsapp sharing button Share
arrow_left sharing button
arrow_right sharing button

C/C++ strncmp - String Compare n Characters

<<Previous

Next >>




strcmp() - String Compare        strcpy() - String Copy        strcat() - String Concatenate




strncmp in C,C++ Programming - Syntax

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

Syntax:
	int strncmp(const char *s1,const char *s2,size_t number);
  • strncmp returns zero value if both strings are same.
  • strncmp returns negative value if string s1 is less than s2.
  • strncmp returns positive value if s1 is greater than s2.

Example C, C++ 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 two strings are same upto 3 characters");
  }
  else
  {
  	printf(" \n The two strings are different");
  }
  return(0);
}

Output if the input is WELCO:

The first string is WELCOME
Enter the second string WELCO
The two strings are same upto 3 characters

Output if the input is WE:

The first string is WELCOME
Enter the second string WE
The two strings are different

strlen() - String Length strlwr() - String Lower       

strncat() - String n Concatenate        strncpy() - String n Copy






<<Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














C/C++ strncmp - String Compare n Characters

<<Previous

Next >>




strcmp() - String Compare        strcpy() - String Copy        strcat() - String Concatenate




strncmp in C,C++ Programming - Syntax

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

Syntax:
	int strncmp(const char *s1,const char *s2,size_t number);
  • strncmp returns zero value if both strings are same.
  • strncmp returns negative value if string s1 is less than s2.
  • strncmp returns positive value if s1 is greater than s2.

Example C, C++ 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 two strings are same upto 3 characters");
  }
  else
  {
  	printf(" \n The two strings are different");
  }
  return(0);
}

Output if the input is WELCO:

The first string is WELCOME
Enter the second string WELCO
The two strings are same upto 3 characters

Output if the input is WE:

The first string is WELCOME
Enter the second string WE
The two strings are different

strlen() - String Length strlwr() - String Lower       

strncat() - String n Concatenate        strncpy() - String n Copy






<<Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy