C Programming Examples
-
C Program to Compare Two Strings
In the world of programming, programmers commonly use string comparison to determine whether two strings are equal or not. In this blog post, we will explore how to write a C program to compare two strings. We will dive into the concept of strings, understand the different ways to compare them, and learn about the intricacies of string comparison in the C programming language. Let’s start and unravel the secrets of comparing strings in C!
Understanding Strings in C
Before we begin comparing strings, let’s have a quick overview of what strings are in the context of C programming. In C, a string is an array of characters terminated by a null character (‘\0’). Each character in the string is represented by its ASCII value, enabling us to perform various operations on strings.
Now that we have a basic understanding of strings, let’s move on to exploring the different methods of comparing strings in C.
The strcmp() Function
C provides us with a built-in function called
strcmp()
that is specifically designed for comparing strings. This function takes two string arguments and returns an integer value based on the comparison result. Let’s take a closer look at the syntax ofstrcmp()
:int strcmp(const char *str1, const char *str2)
The
strcmp()
function compares the stringstr1
with the stringstr2
. Here’s what the return value ofstrcmp()
signifies:If the return value is less than 0, it means
str1
is lexicographically less thanstr2
.If the return value is greater than 0, it means
str1
is lexicographically greater thanstr2
.If the return value is 0, it means
str1
is equal tostr2
.
Let’s look at an example to make things clearer:
#include <stdio.h> #include <string.h> int main() { char str1[20] = "Hello"; char str2[20] = "World"; int result = strcmp(str1, str2); if (result < 0) { printf("str1 is less than str2\n"); } else if (result > 0) { printf("str1 is greater than str2\n"); } else { printf("str1 is equal to str2\n"); } return 0; }
In the above example, we have two strings
str1
andstr2
. We usestrcmp()
to compare them and store the result in theresult
variable. Depending on the value ofresult
, we print the appropriate message. When we run this program, it will outputstr1 is less than str2
because “Hello” comes before “World” lexicographically according to ASCII values.The strncmp() Function
Besides
strcmp()
, C also provides us with another function calledstrncmp()
that allows us to compare a specified number of characters of two strings. The syntax forstrncmp()
is as follows:int strncmp(const char *str1, const char *str2, size_t num)
The
strncmp()
function compares at mostnum
characters of stringstr1
with stringstr2
. The return value follows the same convention asstrcmp()
.Here’s an example that demonstrates the usage of
strncmp()
:#include <stdio.h> #include <string.h> int main() { char str1[20] = "Hello World"; char str2[20] = "Hello World!"; int result = strncmp(str1, str2, 5); if (result < 0) { printf("str1 is less than str2\n"); } else if (result > 0) { printf("str1 is greater than str2\n"); } else { printf("str1 is equal to str2\n"); } return 0; }
In the above example, we compare the first 5 characters of the two strings, “Hello” and “Hello”. As a result, the program outputs
str1 is equal to str2
since the first 5 characters are the same in both strings.Alternative Approaches to String Comparison
While
strcmp()
andstrncmp()
are the most common methods of comparing strings in C, there are alternative approaches that can be useful in certain situations.Using a Loop
One approach is to manually compare the characters of the strings using a loop. We can iterate through each character of the strings and compare them one by one until we find a mismatch or reach the end of either string. Here’s an example:
#include <stdio.h> int strcmp_loop(const char *str1, const char *str2) { int i = 0; while (str1[i] != '\0' || str2[i] != '\0') { if (str1[i] < str2[i]) { return -1; } else if (str1[i] > str2[i]) { return 1; } i++; } return 0; } int main() { char str1[20] = "Hello"; char str2[20] = "World"; int result = strcmp_loop(str1, str2); if (result < 0) { printf("str1 is less than str2\n"); } else if (result > 0) { printf("str1 is greater than str2\n"); } else { printf("str1 is equal to str2\n"); } return 0; }
In the above example, we define a custom function
strcmp_loop()
that compares the stringsstr1
andstr2
character by character. The function uses a loop to iterate through the characters and returns a value based on the comparison result.Converting to ASCII Values
Another alternative approach is to compare the ASCII values of the characters directly. This method works because each character in C is represented by its ASCII value. Here’s an example:
#include <stdio.h> int strcmp_ascii(const char *str1, const char *str2) { int i = 0; while (str1[i] != '\0' || str2[i] != '\0') { if (str1[i] - str2[i] < 0) { return -1; } else if (str1[i] - str2[i] > 0) { return 1; } i++; } return 0; } int main() { char str1[20] = "Hello"; char str2[20] = "World"; int result = strcmp_ascii(str1, str2); if (result < 0) { printf("str1 is less than str2\n"); } else if (result > 0) { printf("str1 is greater than str2\n"); } else { printf("str1 is equal to str2\n"); } return 0; }
In this example, we define the
strcmp_ascii()
function that compares the stringsstr1
andstr2
by subtracting their ASCII values. The function uses a loop similar to the previous approach and provides the same comparison result.Conclusion
In this blog post, we’ve explored how to compare two strings in C. We’ve learned about the
strcmp()
andstrncmp()
functions that are specifically designed for string comparison. We’ve also discussed alternative approaches using loops and direct comparison of ASCII values. All these methods provide us with the means to compare strings and determine their relative order.Whether you’re a beginner just starting your programming journey or an experienced developer looking to refresh your knowledge, understanding string comparison in C is a fundamental skill. By mastering these techniques, you’ll be able to write more complex programs that involve manipulating and analyzing text.
As you continue your programming journey, don’t forget to practice writing C programs and experiment with different string comparison techniques. By doing so, you’ll gain a deeper understanding of strings and sharpen your programming skills.
So keep coding, keep exploring, and keep pushing the boundaries of what you can create with C!