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 without using any library functions like strcmp
.
Problem Statement
Create a C program that:
- Takes two strings as input from the user.
- Compares the two strings manually character by character.
- 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
- Include the Standard Input-Output Library: Use
#include <stdio.h>
for standard input-output functions. - Write the Main Function: Define the
main
function, which is the entry point of every C program. - Declare Variables: Declare variables to store the two input strings and an integer variable for comparison.
- Input the Strings: Use
gets
orscanf
to take input from the user for both strings. - Compare the Strings Manually: Use a loop to iterate through the characters of the two strings and compare them one by one.
- Display the Result: Use
printf
to display the result of the comparison.
C Program to Compare Two Strings Without Using Library 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
Step 1: Declare Variables
- The
str1
andstr2
arrays are declared to store the input strings. Theresult
variable is used to store the comparison result, andi
is used as a loop counter.
Step 2: Input the Strings
- The program prompts the user to enter the first and second strings using
printf
. Thegets
function is used to read the strings, allowing them to include spaces.
Step 3: Manually Compare the Strings
- The program uses a
for
loop to iterate through each character of the two strings:- 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.
Step 4: Display the Result
- The program checks the value of
result
and displays whether the strings are identical, or which one is lexicographically greater using theprintf
function.
Step 5: Return 0
- The
return 0;
statement indicates that the program executed successfully.
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 how to compare two strings manually by iterating through each character and comparing them. It covers basic concepts such as string manipulation, loops, and conditional statements, making it a useful example for beginners learning C programming.