C Program to Compare Two Strings Using Pointers

Introduction

Comparing two strings involves checking if they are identical or if one is lexicographically greater or smaller than the other. In C, you can compare two strings by using pointers to traverse each string character by character. This guide will show you how to write a C program to compare two strings using pointers.

Example:

  • Input: "Hello" and "Hello"

  • Output: The strings are equal.

  • Input: "Hello" and "World"

  • Output: The strings are not equal.

Problem Statement

Create a C program that:

  • Takes two strings as input from the user.
  • Uses pointers to compare the two strings character by character.
  • Displays whether the strings are equal or not.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Declare the Two Strings and Pointer Variables: Declare character arrays to store the two strings and pointers to traverse the strings.
  3. Input the Two Strings: Use gets or fgets to take input from the user.
  4. Compare the Strings Using Pointers: Use pointers to traverse both strings simultaneously and compare each character.
  5. Display the Result: Use printf to display whether the strings are equal or not.

C Program to Compare Two Strings Using Pointers

#include <stdio.h>

int main() {
    // Step 2: Declare the two strings and pointer variables
    char str1[100], str2[100];
    char *ptr1, *ptr2;

    // Step 3: Prompt the user to enter the two strings
    printf("Enter the first string: ");
    gets(str1);  // Use fgets(str1, 100, stdin) if gets is deprecated

    printf("Enter the second string: ");
    gets(str2);  // Use fgets(str2, 100, stdin) if gets is deprecated

    // Initialize pointers to the start of the strings
    ptr1 = str1;
    ptr2 = str2;

    // Step 4: Compare the strings using pointers
    while (*ptr1 != '\0' && *ptr2 != '\0') {
        if (*ptr1 != *ptr2) {
            break;  // If characters differ, break the loop
        }
        ptr1++;
        ptr2++;
    }

    // Step 5: Display the result
    if (*ptr1 == '\0' && *ptr2 == '\0') {
        printf("The strings are equal.\n");
    } else {
        printf("The strings are not equal.\n");
    }

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Step 2: Declare the Two Strings and Pointer Variables

  • The character arrays str1 and str2 are declared to store the two input strings.
  • The pointers ptr1 and ptr2 are used to traverse the two strings.

Step 3: Input the Two Strings

  • The program prompts the user to enter the first and second strings using gets. Note that gets may be unsafe; for safer input, you can use fgets(str1, 100, stdin) and fgets(str2, 100, stdin).

Step 4: Compare the Strings Using Pointers

  • ptr1 is initialized to point to the first character of str1, and ptr2 is initialized to point to the first character of str2.
  • A while loop is used to traverse both strings simultaneously:
    • The loop continues until either ptr1 or ptr2 points to the null terminator ('\0').
    • Inside the loop, the characters pointed to by ptr1 and ptr2 are compared.
    • If the characters differ, the loop breaks, indicating that the strings are not equal.
    • If the characters are the same, both pointers are incremented to move to the next characters.

Step 5: Display the Result

  • After the loop, the program checks if both pointers have reached the null terminator ('\0'):
    • If both strings have reached the end simultaneously, they are equal.
    • Otherwise, the strings are not equal, and the program prints a message accordingly.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example Output 1:

Enter the first string: Hello
Enter the second string: Hello
The strings are equal.

Example Output 2:

Enter the first string: Hello
Enter the second string: World
The strings are not equal.

Conclusion

This C program demonstrates how to compare two strings using pointers. It covers basic concepts such as pointer manipulation, string traversal, and conditional checking, 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