C Programming - strcmp() string comparison

<<Previous

Next >>





strcmp() - String Compare        strcat() - String Concatenate        strlen() - String Length



Function strcmp() in C Programming - Syntax


strcmp(String1, String2) function compares 2 strings and returns an integer value.


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

  • strcmp(String1, String2) returns zero if two strings are the same.
  • strcmp returns a negative value if string String1 is less than String2.
  • strcmp returns a positive value if String1 is greater than String2.

Example C program using strcmp()


#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 = strcmp(first,second );
  printf(" \n The comparison result %d ",result);
  return(0);
}

Output:

The first string is WELCOME
Enter the second string WELCOME
The comparison result 0

strcmp is Case Sensitive

strcmp performs case sensitive comparison. Suppose if 2 strings are almost the same except for the case, then it will return a non-zero value as result. For example, Hello is not the same as hello.


strcmp in Sorting

Using the positive or negative value returned by strcmp, We can sort 2 strings in ascending or descending order and repeat the process to sort any number of strings.


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

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






<<Previous

Next >>




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


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














C Programming - strcmp() string comparison

<<Previous

Next >>





strcmp() - String Compare        strcat() - String Concatenate        strlen() - String Length



Function strcmp() in C Programming - Syntax


strcmp(String1, String2) function compares 2 strings and returns an integer value.


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

  • strcmp(String1, String2) returns zero if two strings are the same.
  • strcmp returns a negative value if string String1 is less than String2.
  • strcmp returns a positive value if String1 is greater than String2.

Example C program using strcmp()


#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 = strcmp(first,second );
  printf(" \n The comparison result %d ",result);
  return(0);
}

Output:

The first string is WELCOME
Enter the second string WELCOME
The comparison result 0

strcmp is Case Sensitive

strcmp performs case sensitive comparison. Suppose if 2 strings are almost the same except for the case, then it will return a non-zero value as result. For example, Hello is not the same as hello.


strcmp in Sorting

Using the positive or negative value returned by strcmp, We can sort 2 strings in ascending or descending order and repeat the process to sort any number of strings.


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

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






<<Previous

Next >>






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

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