0
0
CProgramBeginner · 2 min read

C Program to Compare Strings Without strcmp Function

You can compare two strings in C without 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

Input"hello", "hello"
OutputStrings are equal
Input"apple", "apples"
OutputStrings are not equal
Input"test", "best"
OutputStrings are not equal
🧠

How to Think About It

To compare two strings without using 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

1
Get two input strings.
2
Start from the first character of both strings.
3
Compare characters at the current position.
4
If characters differ, return not equal.
5
If end of both strings is reached together, return equal.
6
If one string ends before the other, return not equal.
💻

Code

c
#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;
}
Output
Strings are equal
🔍

Dry Run

Let's trace comparing "hello" and "hello" through the code

1

Initialize index

i = 0

2

Compare characters at i=0

str1[0] = 'h', str2[0] = 'h' (equal, continue)

3

Compare characters at i=1

str1[1] = 'e', str2[1] = 'e' (equal, continue)

4

Compare characters at i=2

str1[2] = 'l', str2[2] = 'l' (equal, continue)

5

Compare characters at i=3

str1[3] = 'l', str2[3] = 'l' (equal, continue)

6

Compare characters at i=4

str1[4] = 'o', str2[4] = 'o' (equal, continue)

7

Check string ends at i=5

str1[5] = '\0', str2[5] = '\0' (both end, strings equal)

Index istr1[i]str2[i]Equal?
0hhYes
1eeYes
2llYes
3llYes
4ooYes
5\0\0Yes
💡

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

Pointer Increment
c
#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;
}
Uses pointers directly for cleaner code and slightly better performance.
Recursive Comparison
c
#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;
}
Uses recursion to compare characters, elegant but uses more stack space.

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.

ApproachTimeSpaceBest For
Indexing loopO(n)O(1)Simple and clear code
Pointer incrementO(n)O(1)Efficient and clean code
RecursiveO(n)O(n)Elegant but uses more memory
💡
Always check both characters and string ends to avoid false equality.
⚠️
Forgetting to check if both strings end at the same time causes incorrect equality results.