Program to Compare Two Strings in C Programming

program-to-compare-two-strings-in-c-language

Program Description:

Write a Program to Compare Two Strings in C programming language. The program will accept two strings from the user and compares them lexicographically (in dictionary order). We compare the string based on the ASCII values and the program is Case sensitive. So the capital H is not the same as the lowercase h.

📢 This Program is part of the C String Practice Programs series

Here is the example input and output of the program.

Example Input and Output:

Input:

Enter the first string : CProgramming

Enter the second string : CProgramming

Output:

str1:CProgramming and str2:CProgramming are Equal

The Two input strings are equal.

Prerequisites:

Please go through the following articles to better understand this program.

We are going to look at three methods to compare two strings in C Language in this article, They are

  1. Iterative Method: Manual comparison of strings using loops.
  2. User-Defined Function: Using a user-defined function(without strcmp) to compare the strings.
  3. strcat library function: Using the strcmp library function to compare the strings.

Compare Two Strings in C using Iterative Method ( Without strcmp function):

In C, To compare two strings, we can iterate through both strings character by character. If any differences are found, the strings are not equal. If we reach the end of the strings without finding any differences, the strings are considered equal.

Here is the step-by-step explanation of the String comparison program

An iterative method to Compare two strings in C Algorithm:

  1. Declare the two strings named str1 and str2 with SIZE size. ( i.e SIZE = 100 characters)
  2. Ask the user to input the strings for str1and str2and store the input in those variables.
  3. Now we need to compare the two strings. Create a for loop and start iterating through the two strings.
    • FOR Loop – Start with i=0 and continue till str1[i] != '\0' && str2[i] != '\0'.
    • At each iteration, Check if the str1[i] and str2[i] are not equal. If it is true, Then the two strings are Not Equal. break the loop. Otherwise, continue to the next iteration.
  4. Once the above loop is completed, Check if we reached the end of both strings (if(str1[i] == ‘\0’ && str2[i] == ‘\0’)), If so, Then we haven’t found any difference. So Given strings are Equal.
  5. Otherwise, Check if the (str1[i] < str2[i]), Then the str1 is lexicographically smaller than the str2
  6. If the above two cases are not satisfied, Then the str1 is lexicographically greater than the str2.
  7. Print the result on the console.

Program – String Comparision using Iterative Method:

Program Output:

Let’s Compile and Run the Program.

Test case 1: When both strings are equal

The str1 and str2 are equal.

compare-strings-in-c-using-iterative-method

Test case 2: When the str1 is greater than str2

First string Hi is lexicographically greater than the second string hello. Which means the first string Hi comes after the second string in the dictionary order.

Test case 3: When the str1 is smaller than str2

The First string( str1) Rafa is lexicographically smaller than the second string( str2) Roger. Which means the first string Rafa comes before the second string Roger in the dictionary order.

Program to Compare Two Strings using a User defined function in C Language:

Let’s look at the second method, To compare the Two string using a User defined Function (without the strcmp function).

We have defined a function mystrcmp in the above program to compare two strings. Prototype of the mystrcmp is int mystrcmp(char * str1, char * str2).

The mystrcmp() function takes two character pointers. Pointing to the str1 and str2 strings respectively. This function compares two strings by iterating over the all characters of the strings and returns the ASCII difference between the first non-matching character ( str1[i] - str2[i])

This mystrcmp function also returns similar results to the strcmp library function.

  • a Zero(0) if the two strings are equal
  • a Negative value if the str1 is smaller than the str2.
  • a Positive Value if the str1 is greater than the str2.

Program Output:

Compile and run the program using GCC compiler (Any compiler)

compare-two-string-using-user-defined-function

The program is properly generating the results.

Compare two strings in C using the strcmp function:

The C Standard library provides the strcmp function to compare two strings. We already discussed the strcmp function in detailed in the following article.

Here is the syntax of the strcmp function.

Let’s use the in built strcmp function to compare the given strings.

Program Output:

Compile and Run the program.

string-comparision-using-strcmp-func-program-output

As expected, The program is providing the desired results.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Compare Two Strings […]

Leave a Reply