Introduction:
This C program is designed to compare two strings inputted by the user. It checks for equality and determines the lexicographical order of the strings. The program uses a custom function to perform the comparison, illustrating basic string manipulation techniques in C. This functionality is important in many applications, such as sorting, searching, and validating user inputs.
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0; // Remove newline character
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0; // Remove newline character
int cmpResult = strcmp(str1, str2);
if (cmpResult == 0) {
printf("The two strings are equal.\n");
} else if (cmpResult < 0) {
printf("The first string is less than the second string.\n");
} else {
printf("The first string is greater than the second string.\n");
}
return 0;
}
Include Header Files:
#include <stdio.h>
: This header file is included for standard input and output functions likeprintf
andfgets
.#include <string.h>
: This header file is included for string manipulation functions likestrcmp
.
Declare String Arrays:
char str1[100], str2[100];
: Two character arrays are declared to hold the input strings.
Input the First String:
printf("Enter the first string: ");
: Prompts the user for input.fgets(str1, sizeof(str1), stdin);
: Reads a line of text, including spaces, and stores it instr1
.str1[strcspn(str1, "\n")] = 0;
: This line removes the newline character thatfgets
adds to the string.strcspn
finds the length of the string until the newline character, and it is replaced with a null terminator.
Input the Second String:
- Similar to the first string, the program prompts for and reads the second string, storing it in
str2
and removing any newline character.
Compare the Strings:
int cmpResult = strcmp(str1, str2);
: Thestrcmp
function compares the two strings:- It returns 0 if the strings are equal.
- It returns a negative value if
str1
is less thanstr2
(lexicographically). - It returns a positive value if
str1
is greater thanstr2
.
Output the Result of the Comparison:
- The program checks the value of
cmpResult
:- If it is 0, it prints that the two strings are equal.
- If it is less than 0, it prints that the first string is less than the second.
- If it is greater than 0, it prints that the first string is greater than the second.
Return Statement:
return 0;
: Indicates successful completion of the program.
Input/Output Block:
Input:
- The user is prompted to enter two strings for comparison.
Example Input:
Enter the first string: Hello
Enter the second string: Hello, World!
Output:
- The program indicates whether the strings are equal or which one is lexicographically greater.
Example Output:
The first string is less than the second string.
Conclusion:
The string comparison program effectively demonstrates how to compare two strings in C using custom logic. By iterating through the characters of both strings, the program determines their equality and relative order in a clear and efficient manner. This capability is essential in various programming scenarios, including data validation, user input handling, and sorting algorithms. Overall, the program serves as a practical example of string manipulation and comparison techniques, reinforcing fundamental programming concepts in C.