C Program to Compare Two Strings

Introduction

Comparing two strings means checking whether the strings are identical (i.e., they contain the same sequence of characters) or determining their lexicographical order. This guide will show you how to write a C program to compare two strings provided by the user.

Problem Statement

Create a C program that:

  • Takes two strings as input from the user.
  • Compares the two strings.
  • Displays whether the strings are identical or which one is lexicographically greater.

Example:

  • Input:

    • String1 = "Hello"
    • String2 = "Hello"
  • Output: "Strings are identical."

  • Input:

    • String1 = "Apple"
    • String2 = "Banana"
  • Output: "String1 is less than String2."

Solution Steps

  1. Include the Standard Input-Output and String Libraries: Use #include <stdio.h> for standard input-output functions and #include <string.h> for string functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the two input strings.
  4. Input the Strings: Use gets or scanf to take input from the user for both strings.
  5. Compare the Strings:
  • Method 1: Use the strcmp function from the string.h library.
  • Method 2: Manually compare the strings character by character.
  1. Display the Result: Use printf to display the result of the comparison.

C Program Using strcmp Function

#include <stdio.h>
#include <string.h>

int main() {
    // Step 1: Declare variables to hold the two input strings
    char str1[100], str2[100];

    // Step 2: Prompt the user to enter the first string
    printf("Enter the first string: ");
    gets(str1);  // Using gets to read the string including spaces

    // Step 3: Prompt the user to enter the second string
    printf("Enter the second string: ");
    gets(str2);  // Using gets to read the string including spaces

    // Step 4: Compare the strings using strcmp
    int result = strcmp(str1, str2);

    // Step 5: Display the result
    if (result == 0) {
        printf("Strings are identical.\n");
    } else if (result < 0) {
        printf("String1 is less than String2.\n");
    } else {
        printf("String1 is greater than String2.\n");
    }

    return 0;  // Step 6: Return 0 to indicate successful execution
}

C Program Without Using strcmp Function

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the two input strings
    char str1[100], str2[100];
    int i, result = 0;

    // Step 2: Prompt the user to enter the first string
    printf("Enter the first string: ");
    gets(str1);  // Using gets to read the string including spaces

    // Step 3: Prompt the user to enter the second string
    printf("Enter the second string: ");
    gets(str2);  // Using gets to read the string including spaces

    // Step 4: Manually compare the strings character by character
    for (i = 0; str1[i] != '\0' && str2[i] != '\0'; i++) {
        if (str1[i] != str2[i]) {
            result = str1[i] - str2[i];
            break;
        }
    }

    // If strings are identical till now but one string has more characters
    if (result == 0 && (str1[i] != '\0' || str2[i] != '\0')) {
        result = str1[i] - str2[i];
    }

    // Step 5: Display the result
    if (result == 0) {
        printf("Strings are identical.\n");
    } else if (result < 0) {
        printf("String1 is less than String2.\n");
    } else {
        printf("String1 is greater than String2.\n");
    }

    return 0;  // Step 6: Return 0 to indicate successful execution
}

Explanation

Using strcmp Function

  • strcmp: The strcmp function from the string.h library compares str1 and str2 lexicographically:
    • Returns 0 if the strings are identical.
    • Returns a negative value if str1 is less than str2.
    • Returns a positive value if str1 is greater than str2.

Without Using strcmp Function

  • Manual Comparison: The program manually compares the strings by iterating through each character:
    • If a mismatch is found, the comparison stops, and the difference between the characters determines the result.
    • If no mismatch is found, but the strings are of different lengths, the longer string is considered greater.

Steps Explanation:

  1. Declare Variables: The str1 and str2 arrays store the input strings. The result variable is used to store the comparison result.
  2. Input the Strings: The gets function reads the input strings, including spaces, and stores them in str1 and str2.
  3. Compare the Strings: Depending on the approach, either the strcmp function or a manual loop compares str1 and str2.
  4. Display the Result: The program displays the comparison result using the printf function.

Output Example

Example 1:

Enter the first string: Hello
Enter the second string: Hello
Strings are identical.

Example 2:

Enter the first string: Apple
Enter the second string: Banana
String1 is less than String2.

Example 3:

Enter the first string: Cat
Enter the second string: Car
String1 is greater than String2.

Conclusion

This C program demonstrates two methods to compare two strings: using the strcmp function and manually comparing character by character. It covers basic concepts such as string manipulation, loops, and functions, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top