C++ Compare Char Array with String

C - Comparing string literal with character array

strcmp returns 0 when the strings are the same. I have code that uses strcmp comparing character arrays to string literals, and I was quite confused when it wasn't working. Turns out it was wrong for me to assume it would return 1 when the string are the same!

Maybe you've made the same mistake?

C - Compare a char array with string

Use strncmp function, which compares the first n bytes of the string:

if (strncmp(in, "Hello", strlen("Hello")) == 0) ...

Comparing chars one by one in a character array with strcmp

strcmp() takes null-terminated strings, but neither of your char[] arrays are null-terminated.

char test1[2]; // <-- increase this!
char test2[2]; // <-- increase this!
test1[0] = str1[i];
test1[1] = '\0'; // <-- add this!
test2[0] = str2[i];
test2[1] = '\0'; // <-- add this!
int result = strcmp(test1, test2);

Otherwise, you can use strncmp() instead, which does not require null terminators, so no char[] arrays are needed, eg:

char test1 = str1[i];
char test2 = str2[i];
int result = strncmp(&test1, &test2, 1);

Or simply:

int result = strncmp(&str1[i], &str2[i], 1);
// or:
// int result = strncmp(str1+i, str2+i, 1);

How to Compare 2 Character Arrays

but I read somewhere else that I couldnt do test[i] == test2[i] in C.

That would be really painful to compare character-by-character like that. As you want to compare two character arrays (strings) here, you should use strcmp instead:

if( strcmp(test, test2) == 0)
{
printf("equal");
}

Edit:

  • There is no need to specify the size when you initialise the character arrays. This would be better:

    char test[] = "idrinkcoke";
    char test2[] = "idrinknote";

  • It'd also be better if you use strncmp - which is safer in general (if a character array happens to be NOT NULL-terminated).

    if(strncmp(test, test2, sizeof(test)) == 0)

Compare char array element to string literal

"." is a string literal. What you want should be a character constant '.'.

Try this:

#include <stdio.h>
#include <string.h>

int main(void) {
const char S[] = "0.9";
if (S[1] == '.') {
puts("got it");
}
return 0;
}

Alternative (but looks worse) way: access an element of the string literal

#include <stdio.h>
#include <string.h>

int main(void) {
const char S[] = "0.9";
if (S[1] == "."[0]) {
puts("got it");
}
return 0;
}

How to compare two char arrays in C++?

You can't compare the contents of two char* string by comparing their pointers, as in if (z1 == z2). This will (almost) always be false, as the strings are in two different memory locations, so their addresses will be different.

You should use the strcmp() function, which returns zero if the strings are the same. So:

    if (strcmp(z1,z2) == 0)
{
Serial.print("OK");
}
else
{
Serial.print("FAILED");
}

C++ Compare char array with string

Use strcmp() to compare the contents of strings:

if (strcmp(var1, "dev") == 0) {
}

Explanation: in C, a string is a pointer to a memory location which contains bytes. Comparing a char* to a char* using the equality operator won't work as expected, because you are comparing the memory locations of the strings rather than their byte contents. A function such as strcmp() will iterate through both strings, checking their bytes to see if they are equal. strcmp() will return 0 if they are equal, and a non-zero value if they differ. For more details, see the manpage.

Is there a way to compare char and string in c++?

Is the user variable required to be a char array? If it is not you could use the std::string type and the std::string::compare method. It returns 0 if the strings match.

std::string firstUser ("foo");
std::string secondUser ("bar");

if (firstUser.compare(secondUser) != 0) std::cout << "enter right username!";
else std::cout << "well done!";

The compare method has some interesting variations you should check the reference here.



Related Topics



Leave a reply



Submit