You can't (usefully) compare strings using != or ==, you need to use strcmp:

while (strcmp(check,input) != 0)

The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.

Answer from Mysticial on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strcmp-in-c
strcmp() in C %%sep%% %%sitename%% - GeeksforGeeks
December 13, 2025 - It takes two strings (array of ... is used to compare two strings, with syntax: int strcmp(const char *str1, const char *str2);, where str1 and str2 are the strings to compare....
Discussions

Fast String comparisons in C
Two problems I can think of, off the top: Alignment. Some processors might want accesses to be aligned on certain boundaries, so the uint32_t access of an arbitrary string could be problematic. I think you're making an assumption that len is no larger than the size of the largest string. If someone were to pass in a "len" that was much larger than either string (e.g. both strings are in a buffer of a max size, and they pass in the max size), then you could easily walk off the end of the strings in the first loop, since you're not checking for the null terminator. And I just don't know if the semantics of strncmp requires that to not be true (that len can't be arbitrary). Since you'd be potentially comparing beyond the end of the string, you could say two equal strings aren't, since you're looking at characters past the null terminator. More on reddit.com
🌐 r/cprogramming
14
1
October 29, 2023
Fast String comparisons in C
The problem with the cast is that the pointers might be unaligned, potentially causing slowdowns or even crashes in the worst case when dereferenced. To fix that you need to check the alignment of both pointers at entry and work with the largest possible blocks whose alignment requirements match the least aligned string. Other than that, if I recall correctly the C standard allows casting between any pointer type and char or void pointers, so if you implement the aforementioned fix you should be fine. As for the performance of the code, heavily optimized strncmp implementations use compiler intrinsics that produce SIMD instructions. For example on a modern x86-64 CPU with AVX512 you can check whether two properly aligned 64 8-bit blocks are equal with a single instruction. More on reddit.com
🌐 r/learnprogramming
17
7
October 29, 2023
How should I compare strings correctly in C? - TestMu AI Community
However, even when the user retypes the original input, the loop doesn’t stop. Am I using the wrong method for string comparison in C? I’m aware that using check != input may not be the correct approach, but I’m unsure how to properly handle this. Any advice on how to string compare in C the ... More on community.testmuai.com
🌐 community.testmuai.com
0
June 20, 2025
How do I compare two strings alphabetically in c++
Strings have a comparison operator . More on reddit.com
🌐 r/learnprogramming
5
4
February 21, 2021
🌐
W3Schools
w3schools.com › c › ref_string_strcmp.php
C string strcmp() Function
For this comparison characters ... are compared one by one, starting from the left until one of them does not match or the end of a string has been reached. There are three possible scenarios: If the end of both strings has been reached without any mismatches then the function returns zero. At the first mismatch, if the ASCII value of the character in the first ...
🌐
Scaler
scaler.com › home › topics › string comparison in c
String Comparison in C - Scaler Topics
January 11, 2024 - 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.
🌐
freeCodeCamp
freecodecamp.org › news › strcmp-in-c-how-to-compare-strings-in-c
strcmp in C – How to Compare Strings in C
April 27, 2023 - As the function strcmp() gives a boolean output (true/false, or 0/1), I have taken another int variable named result to store the boolean value ( 1 for true and 0 for false). The strcmp() function compares the two strings, and finds out that ...
🌐
Programiz
programiz.com › c-programming › library-function › string.h › strcmp
C strcmp() - C Standard Library
#include <stdio.h> #include <string.h> int main() { char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; int result; // comparing strings str1 and str2 result = strcmp(str1, str2); printf("strcmp(str1, str2) = %d\n", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %d\n", result); return 0; } ... Your builder path starts here. Builders don't just know how to code, they create solutions that matter.
🌐
Reddit
reddit.com › r/cprogramming › fast string comparisons in c
r/cprogramming on Reddit: Fast String comparisons in C
October 29, 2023 -

Hi, I'm working just for fun on a fast comparison for strings written in C, with the intent of being faster than the normal strncmp function, which is currently the code bellow

```
int fast_strncmp(const char *str1, const char *str2, int len) {
    const char *final_pos = (str1 + len) - 4;
    while (str1 < final_pos) {
        // if characters differ, or end of the second string is reached
        if (*((uint32_t *)str1) != *((uint32_t *)str2)) {
            break;
        }
        // move to the block of characters
        str1 += 4;
        str2 += 4;
    }
    final_pos += 4;
    while (str1 < final_pos) {
        if (*str1 != *str2 || *str1 == 0 || *str2 == 0) {
            return *str1 - *str2;
        }
        // move to the next pair of characters
        str1++;
        str2++;
    }
    return 0;
}
```

Is there any clear problem with the code that could make it a bad option for fast string comparisons. When I wrote it a couple of weeks ago, I didn't think there could be any problem with it, but this week I was watching a couple of videos about C programming and it was mentioned that casting an array of 4 uint8_t to a uint32_t could be a problem. I'm even using this function at work and haven't had a single problem or warning, but since I'm planning to make a youtube video about it, I want to guarantee that my code won't be a problem for other people.

On top of that I've made a couple of benchmarks on the performance to be sure it really is fast, so I've compared it to strncmp and an implementation by https://github.com/mgronhol, that I found here: https://mgronhol.github.io/fast-strcmp/, which got me the following results:

EDIT: reddit was not cooperating with me posting the results text in a well formatted way, so here's the link to the file:

https://github.com/BenjamimKrug/fast_string_comparison/blob/main/results.txt

As you can see, running on the STM32 and the ESP32, my algorithm runs faster by a little compared to the fast_compare function by mgronhol, but running on my PC, it's performing terribly. Does anyone know why that is?

You can find more info about the code in my github repository where I put everything related to this test: https://github.com/BenjamimKrug/fast_string_comparison

P.S.: Sorry if this is the wrong subreddit for this kind of thing, I was going to post it on r/programming, but after reading the rules, I saw that maybe it was best to post it here.

EDIT: fixed code formatting

Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › fast string comparisons in c
r/learnprogramming on Reddit: Fast String comparisons in C
October 29, 2023 -

Hi, I'm working just for fun on a fast comparison for strings written in C, with the intent of being faster than the normal strncmp function, which is currently the code bellow

```
int fast_strncmp(const char *str1, const char *str2, int len) {
    const char *final_pos = (str1 + len) - 4;
    while (str1 < final_pos) {
        // if characters differ, or end of the second string is reached
        if (*((uint32_t *)str1) != *((uint32_t *)str2)) {
            break;
        }
        // move to the block of characters
        str1 += 4;
        str2 += 4;
    }
    final_pos += 4;
    while (str1 < final_pos) {
        if (*str1 != *str2 || *str1 == 0 || *str2 == 0) {
            return *str1 - *str2;
        }
        // move to the next pair of characters
        str1++;
        str2++;
    }
    return 0;
}
```

Is there any clear problem with the code that could make it a bad option for fast string comparisons. When I wrote it a couple of weeks ago, I didn't think there could be any problem with it, but this week I was watching a couple of videos about C programming and it was mentioned that casting an array of 4 uint8_t to a uint32_t could be a problem. I'm even using this function at work and haven't had a single problem or warning, but since I'm planning to make a youtube video about it, I want to guarantee that my code won't be a problem for other people.

On top of that I've made a couple of benchmarks on the performance to be sure it really is fast, so I've compared it to strncmp and an implementation by https://github.com/mgronhol, that I found here: https://mgronhol.github.io/fast-strcmp/, which got me the following results:

EDIT: reddit was not cooperating with me posting the results text in a well formatted way, so here's the link to the file:

https://github.com/BenjamimKrug/fast_string_comparison/blob/main/results.txt

As you can see, running on the STM32 and the ESP32, my algorithm runs faster by a little compared to the fast_compare function by mgronhol, but running on my PC, it's performing terribly. Does anyone know why that is?

You can find more info about the code in my github repository where I put everything related to this test: https://github.com/BenjamimKrug/fast_string_comparison

P.S.: Sorry if this is the wrong subreddit for this kind of thing, I was going to post it on r/programming, but after reading the rules, I saw that maybe it was best to post it here.

EDIT: fixed code formatting

🌐
Quora
quora.com › How-can-we-compare-two-strings-efficiently-in-C-or-C
How can we compare two strings efficiently in C or C++? - Quora
So keeping an array of strings to just compare is far worst than keeping an array of integers. ... In C and C++, you can compare two strings efficiently using the standard library function `strcmp()`.
🌐
IBM
ibm.com › docs › en › zos › 3.1.0
strcmp() — Compare strings
We cannot provide a description for this page right now
🌐
TestMu AI Community
community.testmuai.com › ask a question
How should I compare strings correctly in C? - TestMu AI Community
June 20, 2025 - However, even when the user retypes the original input, the loop doesn’t stop. Am I using the wrong method for string comparison in C? I’m aware that using check != input may not be the correct approach, but I’m unsure how to properly handle this. Any advice on how to string compare in C the ...
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › compare strings with strcmp function in c
Compare Strings with strcmp() Function in C - Shiksha Online
December 21, 2023 - If str1 is less than str2, then a negative integer is returned. The comparison is done character by character, starting from the first character of each string, until a mismatch is found or the end of the strings is reached.
🌐
DigitalOcean
digitalocean.com › community › tutorials › compare-strings-in-c-plus-plus
3 Ways to Compare Strings in C++ | DigitalOcean
April 22, 2024 - Results out to be > 0 (greater than zero) when the second string is greater in comparison. ... #include <iostream> #include <string.h> int main() { const char *str_inp1 = "String Match"; const char *str_inp2 = "String Unmatch"; std::cout << "String 1: " << str_inp1 << std::endl; std::cout << "String 2: " << str_inp2 << std::endl; if (strcmp(str_inp1, str_inp2) == 0) std::cout << "\nBoth the input strings are equal."
🌐
LabEx
labex.io › questions › how-to-compare-strings-in-c-136079
How to Compare Strings in C | LabEx
July 25, 2024 - Learn how to compare strings in C using strcmp function, characterbycharacter methods, and wildcard matching for effective programming.
🌐
Cplusplus
cplusplus.com › reference › cstring › strcmp
strcmp - cstring
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. This function performs a binary comparison of the characters. For a function that takes into account locale-specific rules, see strcoll.
🌐
Vultr
docs.vultr.com › clang › standard-library › string-h › strcmp
C string.h strcmp() - Compare Two Strings | Vultr Docs
September 27, 2024 - This code snippet compares str1 and str2. Since both strings are identical, strcmp() returns 0. Understand that strcmp() returns an integer. It returns 0 if both strings are identical. A negative value if the first non-matching character in the first string is less than that in the second string. A positive value if the first non-matching character in the first string is greater than that in the second string. Extend the program to include different comparisons.
🌐
HackerNoon
hackernoon.com › beginners-guide-to-comparing-strings-in-c
Beginner's Guide To Comparing Strings in C# | HackerNoon
January 8, 2024 - Enhance your string comparison skills and write robust, efficient code in C#.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string comparison in c
String Comparison in C: Methods & Examples
May 5, 2025 - In this blog, we'll explore different methods for string comparison in C, from using the standard strcmp() function to writing custom comparison logic with loops and pointers. Each method will be explained step by step, with code examples, comments, ...
🌐
LeetCode
leetcode.com › problems › backspace-string-compare
Backspace String Compare - LeetCode
Can you solve this real interview question? Backspace String Compare - Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. Note that after backspacing an empty text, the text will continue empty.