comparing two string using strcmp and strncmp in c
String Comparison in C: Understanding strcmp and strncmp
To compare two strings in C, you can use the strcmp()
and strncmp()
functions. Both functions compare the two strings lexicographically, meaning that they compare the ASCII values of each character in the strings until they reach a null character ('\0')
or until they find a difference.
strcmp()
The strcmp()
function compares the two strings from beginning to end. If the strings are equal, strcmp()
returns 0
. If the first string is less than the second string, strcmp()
returns a negative number.
If the first string is greater than the second string, strcmp()
returns a positive number.
strncmp()
The strncmp()
function compares the two strings up to a specified number of characters. The third parameter to strncmp()
is the number of characters to compare. If the strings are equal up to the specified number of characters, strncmp()
returns 0
. If the first string is less than the second string up to the specified number of characters, strncmp()
returns a negative number
. If the first string is greater than the second string up to the specified number of characters, strncmp()
returns a positive number.
Example
Example shows how to use the strcmp() and strncmp() functions to compare two strings:
#include <stdio.h>
#include <string.h>
int main() {
char string1[] = "Hello";
char string2[] = "World";
// Compare the two strings using strcmp().
int result = strcmp(string1, string2);
// If the strings are equal, print a message indicating that they are equal.
if (result == 0) {
printf("The two strings are equal.\n");
} else if (result < 0) {
printf("The first string is less than the second string.\n");
} else {
printf("The first string is greater than the second string.\n");
}
// Compare the two strings using strncmp(), comparing the first 3 characters.
result = strncmp(string1, string2, 3);
// If the strings are equal up to the first 3 characters, print a message indicating that they are equal.
if (result == 0) {
printf("The first 3 characters of the two strings are equal.\n");
} else if (result < 0) {
printf("The first 3 characters of the first string are less than the first 3 characters of the second string.\n");
} else {
printf("The first 3 characters of the first string are greater than the first 3 characters of the second string.\n");
}
return 0;
}
Output:
The first string is less than the second string.
The first 3 characters of the two strings are equal.
When to use strcmp() and strncmp()
You should use strcmp()
when you need to compare two strings from beginning to end. You should use strncmp()
when you need to compare two strings up to a specified number of characters.
For example, you might use strcmp()
to compare the names of two files to see if they are the same. You might use strncmp()
to compare the first few characters of a user input string to a known command to see if the user is trying to execute a command.
Further Reading:
Implementation of strncat() in C
add source string at the end of destination string
comparing two string using strcmp and strncmp
Design a function to copy to copy string into destination string using strcpy and strncpy
how to implement implementation of strncpy in c
Design a function to calculate the length of the string using strlen
pre defined string based function
Design a function using my_strcpy and using my_strchr we need to select given char in given string
Enroll Now:
[ C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"
Contact Us:
- For any inquiries, please email us at [info@electro4u.net].
- Follow us on insta [ electro4u_offical_ ] for updates and tips.
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!