String Comparison in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

String comparison in C is crucial in applications like password verification and plagiarism detection. It involves methods like using the strcmp() function, manual comparisons, pointers, and recursion. Understanding how strcmp() works and its alternatives is key to efficiently comparing strings and determining their lexicographical order.

Understanding String Comparison in C

Ever wondered how the websites check if the passwords match when you sign up or how the software detects if there’s any plagiarism, or how the spam filtering in your mail works?

There is one solution to all the above things - String Comparison.

Compare Two Strings in C

Comparing two strings or string comparison in C involves finding out if they are the same or not. This is done by using some built-in function or comparing the strings character by character to determine if they are equal. In case they are not equal, we can also analyze and find out which string is lexicographically (lexicographic order means dictionary order, that is, the words which start from 'a' come before the words that start with 'b' and the earlier is lexicographically smaller than the later, we'll see about this later in the article) larger than the other by the various methods of string comparison in C.

There are four methods for string comparison in C.

  • String comparison by using strcmp() String Library function.
  • String comparison without using strcmp() function .
  • String comparison by using pointers.
  • String comparison by using recursion.

String comparison by Using String Library Function

The string library functions are pre-defined in string.h header file used to do operations on the strings. strcmp() function is used to compare two strings. The strcmp() function takes two strings as input and returns an integer result that can be zero, positive, or negative.

The strcmp() function compares both strings characters. If both strings have the same character at the same index till all of the characters have been compared or the pointer reaches the null character '\0' in both strings then we can say that both strings are equal.

Syntax of the strcmp() Function

In the syntax above, two arguments, str1 and str2, are passed as strings and the return type is int, indicating that strcmp() gives an integer value.

Possible Return Values from the strcmp() Function

Return ValueDescription
0Returns 0, When both strings are exactly the same.
<0The function will return a negative number if the ASCII value of a character in the first string is smaller than the ASCII value of a character in the second string.
>0The function will return a positive value if a character's ASCII value in the first string is bigger than a character's ASCII value in the second string.

Example

Output:

You can run and check your code in this Online C Compiler.

Analysis of the Program

  • We've declared two char arrays, str1, and str2, respectively then take input for them.
  • The strcmp() function, is used to compare the strings (str1,str2). The strings str1 and str2 will be compared using this function. If the function returns a value 0, it signifies that the strings are equal otherwise, strings are not equal.

String Comparison without Using strcmp() Function

String comparison in C is also possible by using without strcmp() function. We could compare the two strings by traversing each index using a loop and comparing each character one by one.

Output:

Explanation:

  • In the code example, We've declared two char arrays and are taking user input as strings.
  • We've built a compareTwoString() function that takes two user input strings as an argument and compares character by character using a while loop. If the function returns 0, then the strings are equal otherwise, strings are not equal.

String Comparison by Using Pointers

String comparison in C also possible by using pointers. In this approach, we use pointers to traverse the strings and then compare the characters pointed by the pointer.

Output:

Explanation:

  • In the code example, We’ve declared two char arrays str1 ,str2 and then take input for them.
  • A compareTwoString() function has been defined, which takes two char pointers as an argument. The address of str1 is held by the 'a' pointer, whereas the address of str2 is held by the 'b' pointer. We've constructed a while loop inside the function that will run until the pointer a or b does not reach a null character.

Using Recursion

We can use recursion to compare two strings, so we'll calculate the lengths of both strings and the compareTwoString function will keep calling itself until the condition becomes false.

Output:

Explanation:

  • In the code example, We’ve declared two char arrays str1 ,str2 and take input for them.
  • A compareTwoString() calculates the length of both string using strlen() function.
  • The compareTwoString() function will return 0 if the lengths of both strings are not equal.
  • If the lengths of both strings are equal then we will compare str1[i] with str2[i] and if str1[i] equals str2[i] we will increase the value of c and i. then compareTwoString(str1, str2) keeps calling itself until i < l1 is false.
  • compareTwoString() function gives 1 if c is equal to i as c counts the number of equal characters at the same index. If c becomes equal to i this specify strings are equal.
  • compareTwoString() function returns 0 if strings are not equal otherwise 1 if strings are equal.

Conclusion

  • A string is a group of characters and the direct comparison of two strings is not possible in C.
  • We can compare two strings in C using a variety of approaches.
  • The two strings to be checked must be compared character by character.
  • We can compare two strings using strcmp() string library function which returns 0 if two strings are not equal.
  • We can compare two strings without string library function also using loops.
  • We can also compare two strings using pointers or recursion.