The strcmp()
function in C is a standard library function that compares two strings. It is part of the C standard library (string.h
). This function is useful for comparing the lexicographical order of two strings.
Table of Contents
- Introduction
strcmp()
Function Syntax- Understanding
strcmp()
Function - Examples
- Comparing Two Identical Strings
- Comparing Different Strings
- Real-World Use Case
- Conclusion
Introduction
The strcmp()
function compares two null-terminated strings lexicographically. It returns an integer less than, equal to, or greater than zero if the first string is found to be less than, equal to, or greater than the second string, respectively.
strcmp() Function Syntax
The syntax for the strcmp()
function is as follows:
int strcmp(const char *str1, const char *str2);
Parameters:
str1
: A pointer to the first null-terminated string to be compared.str2
: A pointer to the second null-terminated string to be compared.
Returns:
- The function returns an integer:
- Less than zero if
str1
is less thanstr2
. - Zero if
str1
is equal tostr2
. - Greater than zero if
str1
is greater thanstr2
.
- Less than zero if
Understanding strcmp() Function
The strcmp()
function performs a lexicographical comparison of the two strings, which means it compares the strings character by character using the ASCII values of the characters. The comparison stops when a difference is found or the end of the strings is reached.
Examples
Comparing Two Identical Strings
To demonstrate how to use strcmp()
to compare two identical strings, we will write a simple program.
Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, World!";
char str2[] = "Hello, World!";
// Compare the strings using strcmp
int result = strcmp(str1, str2);
// Print the result
printf("Comparison result: %d\n", result);
return 0;
}
Output:
Comparison result: 0
Comparing Different Strings
This example shows how to use strcmp()
to compare different strings.
Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, World!";
char str2[] = "Hello, Ramesh!";
// Compare the strings using strcmp
int result = strcmp(str1, str2);
// Print the result
if (result < 0) {
printf("\"%s\" is less than \"%s\"\n", str1, str2);
} else if (result > 0) {
printf("\"%s\" is greater than \"%s\"\n", str1, str2);
} else {
printf("\"%s\" is equal to \"%s\"\n", str1, str2);
}
return 0;
}
Output:
"Hello, World!" is less than "Hello, Ramesh!"
Real-World Use Case
Sorting an Array of Strings
In real-world applications, the strcmp()
function can be used to sort an array of strings lexicographically.
Example: Sorting an Array of Strings
#include <stdio.h>
#include <string.h>
void sortStrings(char arr[][20], int n) {
char temp[20];
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(arr[i], arr[j]) > 0) {
strcpy(temp, arr[i]);
strcpy(arr[i], arr[j]);
strcpy(arr[j], temp);
}
}
}
}
int main() {
char arr[][20] = {"Ramesh", "Suresh", "Mahesh", "Naresh", "Kalpesh"};
int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array of strings
sortStrings(arr, n);
// Print the sorted array
printf("Sorted array of strings:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
Output:
Sorted array of strings:
Kalpesh
Mahesh
Naresh
Ramesh
Suresh
Conclusion
The strcmp()
function is used for comparing strings in C. By understanding and using this function correctly, you can efficiently compare and sort strings in your programs. Always ensure that the strings are null-terminated to prevent undefined behavior. This function is particularly useful for tasks that involve sorting, searching, and lexicographical comparisons of strings.
Comments
Post a Comment
Leave Comment