As mentioned by PaulMcKenzie, it seems that you apply concepts of other languages directly to the c programming language, mostly by guessing. If you do not have a book to start with, use at least google to find some tutorials. For example, I entered "c strings introduction" and got - among others - this link, which explains a lot of the issues you are currently having.

The following does not answer your question or even solve your homework, but maybe it gives you enough hints to start.

A "string" in c is an array of characters, terminated by a 0 (often written as '\0'.

String literals are enclosed in double quotes, i.e. "hello" (not 'hello').

Single characters of a string can be accessed by an index, e.g. char c = ch[0] of char c = ch[5] or int i=3; char c = ch[i].

A loop that iterates through the characters of a string and looks for an A could look as follows:

int i=0;
int found = 0;
while (!found && i < 80 && ch[i] != '\0') {
   if (ch[i] == 'A')
     found = 1;

   i++;
} 

This is not meant as an answer; It is just that I am a beginner, too (though in other fields than C); and beginners should help beginners :-) Have fun learning C, use google and good books, and be aware that it will take some time.

Answer from Stephan Lechner on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-to-compare-two-strings-without-using-strcmp-function
C Program to Compare Two Strings Without Using strcmp() - GeeksforGeeks
December 5, 2024 - The most straightforward method to compare two strings without using strcmp() function is by using a loop to compare the difference between ASCII values of each character in corresponding position.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-program-to-compare-two-strings
C program to Compare Two Strings without using strcmp()
April 4, 2025 - /* without using strcmp() */ #include <stdio.h> #include <string.h> int main() { char Str1[100], Str2[100]; int result, i; printf("\n Please Enter the First String : "); gets(Str1); printf("\n Please Enter the Second String : "); gets(Str2); for(i = 0; Str1[i] == Str2[i] && Str1[i] == '\0'; i++); if(Str1[i] < Str2[i]) { printf("\n str1 is Less than str2"); } else if(Str1[i] > Str2[i]) { printf("\n str2 is Less than str1"); } else { printf("\n str1 is Equal to str2"); } return 0; }
Discussions

How do I properly compare strings in C? - Stack Overflow
I am trying to get a program to let a user enter a word or character, store it, and then print it until the user types it again, exiting the program. My code looks like this: #include More on stackoverflow.com
๐ŸŒ stackoverflow.com
Compraring Char array without using strcmp in C - Stack Overflow
I am looking for a way to compare 2 char arrays without strcmp. Is this the way to go? Or am I missing something? When I compiled it, if I type in the same strings in both, the program gets stuck a... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - proper way of comparing two strings other than strcmp - Stack Overflow
I was reading the proper way of comparing two strings is to use the strcmp() function. However I am not sure why the first comparison below would leads to the condition True (i.e. the contents insi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Compare two strings character by character in C - Stack Overflow
I have one 'simple' (I hope) question. I am actually coding some little program and I need to compare two strings, same length, but different letters like Eagle and Hdjoh I want to compare the fi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Quora
quora.com โ€บ How-can-you-compare-two-strings-without-using-the-strcmp-function-in-C-or-C
How to compare two strings without using the strcmp() function in C or C++ - Quora
[code]int StringComparing(char* str1, char* str2) { int i; if (str1==NULL && str2==NULL) return 0; if (str1==NULL) return -1; if (str2==NULL) return 1; for (i=0; str1[i]!=0 || str2[i]!=0; ++i) { char c1=str1[i]...
Top answer
1 of 2
1

Literal strings in C are really arrays of characters, whose life-time last the entire run-time of the program.

When you get a pointer to a literal string, you get a pointer to its first character (it's normal array-to-pointer decay).

Also, the C specification allows compilers to reuse literals. So for example all instances of "oliver bier" can (and most likely will) be the exact same array, and the pointers to its first character will then of course also be the same.

That's the reason that the comparison x == "oliver bier" will work.

But if you change it to:

char x[] = "oliver bier";

Then the comparison will no longer work, as the pointer to the array x and the pointer to the array "oliver bier" will be different.

2 of 2
1

Most of the time, calling a function like strcmp is the proper and the only way of reliably comparing two strings. Most of the time, checking pointer equality is not a reliable way.

The problem is that two different pointers can point to two different memory regions that contain separate copies of the same string. You can have this:

    +---+        +---+---+---+---+---+---+---+---+---+---+---+---+
p1: | *--------> | o | l | i | v | e | r |   | b | i | e | r |\0 |
    +---+        +---+---+---+---+---+---+---+---+---+---+---+---+

    +---+        +---+---+---+---+---+---+---+---+---+---+---+---+
p2: | *--------> | o | l | i | v | e | r |   | b | i | e | r |\0 |
    +---+        +---+---+---+---+---+---+---+---+---+---+---+---+

Or you can have this:

    +---+        +---+---+---+---+---+---+---+---+---+---+---+---+
p3: | *--------> | o | l | i | v | e | r |   | b | i | e | r |\0 |
    +---+        +---+---+---+---+---+---+---+---+---+---+---+---+
                   ^
    +---+          |
p4: | *------------'
    +---+

p1 and p2 point to different strings, so the pointers will compare unequal. p3 and p4 point to the same string, so the pointers will compare equal.

If the pointers compare equal, obviously strcmp will say the strings are equal, too. But if the pointers are different, the strings might be the same (as in p1 and p2), or they might be different.

Sometimes people write things like

if(str1 == str2 || strcmp(str1, str2) == 0)

This checks to see whether the two strings str1 and str2 are the same. If the pointers are equal, then the strings are the same, and only if the pointers are not equal does the code perform the (more expensive) call to strcmp to check the actual, pointed-to characters.

When you have two string literals in your program that happen to be the same, like

char *w = "oliver bier";
char *w2 = "oliver bier";

or

char *w = "oliver bier";
...
if(w == "oliver bier") { ... }

you can't predict, in general, whether their pointers will be the same or different, whether the compiler was clever enough to have one in-memory copy of the string do double duty for its use in multiple places. Once upon a time this "cleverness" was quite rare, although I gather that today it's pretty common.

๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ string comparison in c
String Comparison in C - Scaler Topics
January 11, 2024 - 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.
Find elsewhere
๐ŸŒ
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

๐ŸŒ
Reddit
reddit.com โ€บ r/c_language โ€บ c program to compare two strings without using strcmp() function
r/c_language on Reddit: C program to COMPARE two strings WITHOUT using strcmp() function
June 10, 2017 - Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-fastest-way-to-compare-two-strings-for-equality-in-C-C-without-using-the-strcmp-and-strlen-functions-from-the-string-h-library
What is the fastest way to compare two strings for equality in C/C++ without using the strcmp() and strlen() functions from the string.h library? - Quora
Answer (1 of 2): Iโ€™m not sure how optimized strcmp is cause usual implementation simply compares chars, one by one till \0 is detected (it actually subtracts two chars and returns result). And, btw, does not need strlen. More advanced implementation works with native types, eg reads 32 bits from...
Top answer
1 of 3
8

The following code is completely ok in C

No, Not at all.

In your code

  if(ch=="a")

is essentially trying to compare the value of ch with the base address of the string literal "a",. This is meaning-and-use-less.

What you want here, is to use single quotes (') to denote a char literal, like

  if(ch == 'a')

NOTE 1:

To elaborate on the difference between single quotes for char literals and double quotes for string literal s,

For char literal, C11, chapter ยง6.4.4.4

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'

and, for string literal, chapter ยง6.4.5

Acharacter string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz".


NOTE 2:

That said, as a note, the recommend signature of main() is int main(void).

2 of 3
5

I wouldn't say the code is okay in either language.

'a' is a single character. It is actually a small integer, having as its value the value of the given character in the machine's character set (almost invariably ASCII). So 'a' has the value 97, as you can see by running

char c = 'a';
printf("%d\n", c);

"a", on the other hand, is a string. It is an array of characters, terminated by a null character. In C, arrays are almost always referred to by pointers to their first element, so in this case the string constant "a" acts like a pointer to an array of two characters, 'a' and the terminating '\0'. You could see that by running

char *str = "a";
printf("%d %d\n", str[0], str[1]);

This will print

97 0

Now, we don't know where in memory the compiler will choose to put our string, so we don't know what the value of the pointer will be, but it's safe to say that it will never be equal to 97. So the comparison if(ch=="a") will always be false.

When you need to compare a character and a string, you have two choices. You can compare the character to the first character of the string:

if(c == str[0])
     printf("they are equal\n");
else printf("confusion\n");

Or you can construct a string from the character, and compare that. In C, that might look like this:

char tmpstr[2];
tmpstr[0] = c;
tmpstr[1] = '\0';

if(strcmp(tmpstr, str) == 0)
     printf("they are equal\n");
else printf("confusion\n");

That's the answer for C. There's a different, more powerful string type in C++, so things would be different in that language.

๐ŸŒ
StudyMite
studymite.com โ€บ blog โ€บ comparing-strings-in-c
Comparing strings in C | StudyMite
4 days ago - We will learn how to write a program for comparing strings in c using various techniques like built-in functions, user-defined functions, and pointers.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ problem with "comparing" string (c)
r/learnprogramming on Reddit: Problem with "comparing" string (C)
May 16, 2022 -

I am trying to write a program and I've run into a compilation error.

--------------------------------------------

CODE:

-------------------------------------------

#include <stdio.h>

#include <cs50.h>

int main(void)

{

string country = get_string("Country: ");

if (country == "england")

{

printf("TEST\n");

}

}

-----------------------------------------------

ERROR MESSAGE:

--------------------------------------------

countries.c:9:17: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]

if (country == "england")

^ ~~~~~~~~~

1 error generated.

make: *** [<builtin>: countries] Error 1

------------------------------------------------------------------

This seems to happen whenever i put a string in an if statement. Any help?

Thank you.

๐ŸŒ
Medium
medium.com โ€บ @supernerdd7 โ€บ comparing-strings-in-c-857539988e51
Comparing strings in C++. Abstract | by Rohan Rai | Medium
March 13, 2022 - In its functioning, it is more or less similar to strcmp() but it takes another argument as integer highlighting the maximum number of characters to be compared ยท Here it can be observed that since the number of characters to be compared is 6, thus the output of the program will be Both strings are equal.
๐ŸŒ
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

๐ŸŒ
CCS, Inc.
ccsinfo.com โ€บ forum โ€บ viewtopic.php
CCS :: View topic - Compare a character of a string
January 8, 2021 - Profile Log in to check your private messages Log in ยท CCS does not monitor this forum on a regular basis