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
- Include the Standard Input-Output and String Libraries: Use
#include <stdio.h>for standard input-output functions and#include <string.h>for string functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the two input strings.
- Input the Strings: Use
getsorscanfto take input from the user for both strings. - Compare the Strings:
- Method 1: Use the
strcmpfunction from thestring.hlibrary. - Method 2: Manually compare the strings character by character.
- Display the Result: Use
printfto 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
strcmpfunction from thestring.hlibrary comparesstr1andstr2lexicographically:- Returns
0if the strings are identical. - Returns a negative value if
str1is less thanstr2. - Returns a positive value if
str1is greater thanstr2.
- Returns
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:
- Declare Variables: The
str1andstr2arrays store the input strings. Theresultvariable is used to store the comparison result. - Input the Strings: The
getsfunction reads the input strings, including spaces, and stores them instr1andstr2. - Compare the Strings: Depending on the approach, either the
strcmpfunction or a manual loop comparesstr1andstr2. - Display the Result: The program displays the comparison result using the
printffunction.
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.