C Program to Compare Strings Without strcmp Function
strcmp by looping through each character and checking if they differ or if one ends before the other, returning 1 if equal or 0 otherwise.Examples
How to Think About It
strcmp, check each character one by one from the start. If characters differ at any position, the strings are different. If one string ends before the other, they are different. If all characters match and both end together, the strings are equal.Algorithm
Code
#include <stdio.h> int compareStrings(const char *str1, const char *str2) { int i = 0; while (str1[i] != '\0' && str2[i] != '\0') { if (str1[i] != str2[i]) { return 0; // Not equal } i++; } if (str1[i] == '\0' && str2[i] == '\0') { return 1; // Equal } else { return 0; // Not equal } } int main() { const char *s1 = "hello"; const char *s2 = "hello"; if (compareStrings(s1, s2)) { printf("Strings are equal\n"); } else { printf("Strings are not equal\n"); } return 0; }
Dry Run
Let's trace comparing "hello" and "hello" through the code
Initialize index
i = 0
Compare characters at i=0
str1[0] = 'h', str2[0] = 'h' (equal, continue)
Compare characters at i=1
str1[1] = 'e', str2[1] = 'e' (equal, continue)
Compare characters at i=2
str1[2] = 'l', str2[2] = 'l' (equal, continue)
Compare characters at i=3
str1[3] = 'l', str2[3] = 'l' (equal, continue)
Compare characters at i=4
str1[4] = 'o', str2[4] = 'o' (equal, continue)
Check string ends at i=5
str1[5] = '\0', str2[5] = '\0' (both end, strings equal)
| Index i | str1[i] | str2[i] | Equal? |
|---|---|---|---|
| 0 | h | h | Yes |
| 1 | e | e | Yes |
| 2 | l | l | Yes |
| 3 | l | l | Yes |
| 4 | o | o | Yes |
| 5 | \0 | \0 | Yes |
Why This Works
Step 1: Compare characters one by one
The code checks each character at the same position in both strings using a loop with while.
Step 2: Detect difference early
If any characters differ, the function returns 0 immediately, meaning strings are not equal.
Step 3: Check string lengths
After the loop, if both strings end at the same position (\0), they are equal; otherwise, not equal.
Alternative Approaches
#include <stdio.h> int compareStrings(const char *str1, const char *str2) { while (*str1 && *str2) { if (*str1 != *str2) { return 0; } str1++; str2++; } return (*str1 == '\0' && *str2 == '\0'); } int main() { if (compareStrings("apple", "apple")) printf("Strings are equal\n"); else printf("Strings are not equal\n"); return 0; }
#include <stdio.h> int compareStrings(const char *str1, const char *str2) { if (*str1 == '\0' && *str2 == '\0') return 1; if (*str1 != *str2) return 0; return compareStrings(str1 + 1, str2 + 1); } int main() { if (compareStrings("test", "test")) printf("Strings are equal\n"); else printf("Strings are not equal\n"); return 0; }
Complexity: O(n) time, O(1) space
Time Complexity
The program compares characters one by one until a difference or string end, so it runs in linear time relative to the shortest string length.
Space Complexity
The program uses constant extra space, only a few variables for indexing or pointers.
Which Approach is Fastest?
Pointer increment method is usually fastest due to less indexing overhead; recursion is elegant but uses more stack space.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Indexing loop | O(n) | O(1) | Simple and clear code |
| Pointer increment | O(n) | O(1) | Efficient and clean code |
| Recursive | O(n) | O(n) | Elegant but uses more memory |