String Comparison in C

String Comparison in C. String comparison is a common operation in programming, String comparison is used to check whether two strings are equal or not. In the C language, comparing strings involves checking each character in the String to ensure the match. In this article, we will write a C program for String comparison.

Understanding String Comparison in C

In C, strings are the arrays of characters(char data type) terminated by a null character(‘\0’). To compare to strings, we need to examine each corresponding character in both strings until we find a mismatch or reach the end of either string. The comparison is case sensitive, that is upper case and lower case characters are written as distinct.

C Program for String Comparison

Let’s write a simple C program that compares strings by using a custom string comparison function as well as by using the standard library function ‘strcmp()’.


C Program for String Comparison

Explanation of C Program for String Comparison

  • The function customStrcmp iterates through the characters of both strings until a mismatch is found or the end of either string is reached. It returns the difference between the ASCII values of the mismatched characters.
  • Inside the main function, there are two strings declared- strA, and strB.
  • We used a standard library function strcmp() to compare the strings and print the result.
  • We also use the custom function customStrcmp() to compare the strings and print the result.

Happy Coding

See Also

1 thought on “String Comparison in C”

Leave a Comment