Comparing strings in C

Written by

Pooja Rao

Program to Compare Strings in C c

If s1 and s2 are two strings, then comparing them would yield either of the following results:

  1. s1 and s2 are equal.
  2. s1 > s2, that is ASCII value of s1 is greater than s2 which means alphabetically also s1 would come previous to s2.
  3. s1 < s2, that is ASCII value of s1 is smaller than s2 which means alphabetically also s1 would come after s2.

First let us see the simpler approach of using pre-defined C functions.

Approach 1: Using the functions strcmp() from string.h.

  • strcmp() compares two given input string. It is defined under h library.
  • Syntax:  int strcmp (const char* str1, const char* str2);

The strcmp() function takes two strings and return an integer value.  The integer             values returned could be one of the below mentioned.

 Return Values from strcmp()

Return Value Remarks
0 if both strings are equal
Negative integer if the ASCII value of first unmatched character is less than second.
Positive integer if the ASCII value of first unmatched character is greater than second.
  • The strcmp() compares two strings character by character. If the first character of

two strings are equal, next character of two strings are compared. This continues

until the corresponding characters of two strings are different or a null character ‘\0’

is encountered.

Code:

#include <stdio.h>

#include <string.h>

int main()

{

char *str1, *str2;

int size = 100;

printf("Enter first string\n");

str1 = (char*)malloc(size);

getline(&str1, &size, stdin);

printf("Enter second string\n");

str2 = (char*)malloc(size);

getline(&str2, &size, stdin);

if (strcmp(str1,str2) == 0)

printf("The strings are equal.\n");

else

printf("The strings are not equal.\n");

return 0;

}

Output:

Case 1: Strings are equal.

Enter first string

hello!

Enter second string

hello!

The strings are equal.

 

Case 2:  Strings are unequal.

Enter first string

Hello!

Enter second string

hello!

The strings are not equal.

 

In the above example, the strings are unequal because uppercase H was used in first string and lowercase h in second, hence.

Approach 2:  Without using pre-defined functions of C

  • We create a function string_compare in order to check if the strings are of equal length or not.
  • First the length of strings is compared in order to check if equal or not, it is the first criteria.
  • If equal in length, call to user defined function is made else error message is displayed to the user.
  • The function initialises a counter c to traverse both strings character by character for comparison checks.
  • If the characters are equal however they are null, meaning end of string we come out of while loop .
  • We traverse and compare each element of array and increment the counter c till end of string is reached.
  • We proceed to check if strings are equal or not by checking if end of string for both strings has been reached or not; if yes strings are equal and return 0 indicates the same; else a value -1 is returned indicating unequal strings.
  • You can optionally set a flag variable initially to 0; once strings are found to be equal change the status of flag to 1 indicating success.
  • Else, you can compare the cth character of both strings to see which string is larger and which is smaller.

Code:

#include <stdio.h>

#include <string.h>

int string_compare(char s1[], char s2[])

{

int c = 0, flag = 0;

while (s1[c] == s2[c])

{

if (s1[c] == '\0' || s2[c] == '\0')

break;

else

c++;

}

if (s1[c] == '\0' && s2[c] == '\0')

{

flag = 1;

return 0;

}

else

{

if(flag == 0)

{

if (s1[c]>s2[c])

printf("String 1 is greater than string 2\n");

else

printf("String 2 is greater than string 1\n");

}

return -1;

}

}

int main()

{

char *str1, *str2;

int size = 100, l1 , l2;

printf("Enter first string\n");

str1 = (char*)malloc(size);

l1 = getline(&str1, &size, stdin);  //storing no.of block read in l1

printf("Enter second string\n");

str2 = (char*)malloc(size);

l2 = getline(&str2, &size, stdin); //storing no.of block read in l1

l1--;   /*Since getline function has stores '\n' as last character which needs to

be excluded from string length calaculation. */

l2--;

if (l1 == l2)

{

if (string_compare(str1,str2) == 0)

printf("The strings are equal.\n");

else

printf("The strings are not equal.\n");

}

else

printf("The length of strings are not equal.\n");

return 0;

}

Output:

Case 1: Strings are equal

Enter first string

why?

Enter second string

why?

The strings are equal.

 

Case 2: Strings are not equal because of length

Enter first string

where are you ?

Enter second string

where are you

The length of strings are not equal.

 

Here, the ‘?’ character is missing in second string which makes length of string 2 on shorter than string 1.

 

 

Case 3: Strings are not equal because of characters

Enter first string

WHY

Enter second string

WHy

String 2 is greater than string 1

The strings are not equal.

 

 

Here the last character of second string ‘y’ is in lowercase, unlike uppercase as in string 1. Also ASCII value of ‘y’ > ‘Y’.

Approach 3: Using Pointers

Code:

#include <stdio.h>

#include <string.h>

int string_compare(char *str1, char *str2)

{

while (*str1 == *str2)

{

if (*str1 == '\0' || *str2 == '\0')

break;

str1++;

str2++;

}

if (*str1 == '\0' && *str2 == '\0')

return 0;

else

return -1;

}

int main()

{

char *str1, *str2;

int size = 100, l1 , l2;

printf("Enter first string\n");

str1 = (char*)malloc(size);

l1 = getline(&str1, &size, stdin);  //storing no.of block read in l1

printf("Enter second string\n");

str2 = (char*)malloc(size);

l2 = getline(&str2, &size, stdin); //storing no.of block read in l1

l1--;   /*Since getline function has stores '\n' as last character which needs to

be excluded from string length calaculation. */

l2--;

if (l1 == l2)

{

if (string_compare(str1,str2) == 0)

printf("The strings are equal.\n");

else

printf("The strings are not equal.\n");

}

else

printf("The length of strings are not equal.\n");

return 0;

}

Output:

Case 1: Strings are equal

Enter first string

help

Enter second string

help

The strings are equal.

 

Case 2: Strings are not equal because of length.

Enter first string

help

Enter second string

help

The strings are equal.

 

Case 3: Strings are not equal because of characters.

 

Enter first string

Hello!

Enter second string

Hellp!

The strings are not equal.

 

Thus, we have seen various methods to perform string comparison operation in C.

Comparing strings in C