Write a C program to compare two strings using strcmp() library function. How to use strcmp() string library function. Comparing two different strings using inbuilt string library function in C programming.
Example:
Input string1: CodeforWin
Input string2: CodeforWin
Output: Both strings are equal
Required knowledge
Basic C programming, Loop, String, FunctionsProgram to compare two strings
/**
* C program to compare two string without using string library functions
*/
#include <stdio.h>
#define MAX_SIZE 100 //Maximum size of the string
/* Function declarations */
int compare(char * string1, char * string2);
int main()
{
char string1[MAX_SIZE], string2[MAX_SIZE];
int res;
/* Reads two strings from user */
printf("Enter first string: ");
gets(string1);
printf("Enter second string: ");
gets(string2);
/* Calls function to compare both strings and stores result in res */
res = compare(string1, string2);
if(res == 0)
{
printf("Both strings are equal.\n");
}
else if(res == -1)
{
printf("First string is lexicographically smaller than second.\n");
}
else
{
printf("First string is lexicographically greater than second.\n");
}
return 0;
}
/**
* Compares two strings lexicographically.
* Returns 0 if both strings are equal,
* -1 if first string is smaller
* otherwise returns 1
*/
int compare(char * string1, char * string2)
{
int i=0;
/* Runs till both strings are equal */
while(string1[i] == string2[i])
{
if(string1[i] == '\0' || string2[i] == '\0')
break;
i++;
}
if(string1[i-1] == '\0' && string2[i-1]=='\0')
return 0;
else if(string1[i] > string2[i])
return 1;
else if(string1[i] < string2[i])
return -1;
}
Note: You can also use predefined library function strcmp(str1, str2) to compare two strings. This function returns -1 if first string is lexicographically smaller than second string, returns 0 if both string are lexicographically equal else returns 1 if first string is lexicographical greater than second string. This function is present under string.h header file.
Program to compare strings using strcmp()
/**
* C program to compare two string using strcmp() function
*/
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 //Maximum size of the string
int main()
{
char string1[MAX_SIZE], string2[MAX_SIZE];
int res;
/* Reads two strings from user */
printf("Enter first string: ");
gets(string1);
printf("Enter second string: ");
gets(string2);
/* Calls strcmp() to compare both strings and stores result in res */
res = strcmp(string1, string2);
if(res == 0)
{
printf("Both strings are equal.\n");
}
else if(res == -1)
{
printf("First string is lexicographically smaller than second.\n");
}
else
{
printf("First string is lexicographically greater than second.\n");
}
return 0;
}
Output
Enter first string: CODEFORWIN
Enter second string: codeforwin
First string is lexicographically smaller than second.
Enter second string: codeforwin
First string is lexicographically smaller than second.
Happy coding ;)
You may also like
- String programming exercises and solution.
- C program to find length of a string.
- C program to copy one string to another string.
- C program to concatenate two strings.
- C program to convert uppercase string to lowercase string.
- C program to convert lowercase string to uppercase string
- C program to find reverse of a given string.
- C program to check whether a string is palindrome or not.
- C program to find first occurrence of a character in string.
- C program to remove first occurrence of a character from the string.