Differences between C++ string == and compare()?
This is what the standard has to say about operator==
21.4.8.2 operator==
template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const basic_string<charT,traits,Allocator>& rhs) noexcept;
Returns: lhs.compare(rhs) == 0.
Seems like there isn't much of a difference!
How do I properly compare strings in C?
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.
How to compare the string in C? What does this error warning: comparison between pointer and integer means?
There are several problems with your code. username
and pass
are arrays (i.e. pointers) so you shouldn't pass their address with the &
operator. Secondly since it seems you would like the username and password to be more than one character long you should use string comparisons from string.h
#include "string.h"
#include <stdio.h>
void login()
{
char username[100];
char pass[100];
int i = 0;
while( i < 3){
printf("Enter the username : ");
scanf("%s", username); //username is an array and therefore a pointer to the first element so no & is necessary
printf("Password : "); //username is an array and therefore a pointer to the first element so no & is necessary
scanf("%s", pass);
printf("%s %s \n", username, pass);
if ((0 == strcmp(username, "a")) && //use string comparison so can comapre more than one character
(0 == strcmp(pass, "a")))
{
printf("You are sucessfull login into your account\n");
//BilaDahLogin();
break;
}
else{
printf("The username or password incorrect\nTry again\n");
i += 1;
}
}
}
void main(void)
{
login();
}
C#: What is the difference between CompareTo(String) and Equals(String)?
From MSDN:
string.CompareTo:
Compares this instance with a specified object or String and returns
an integer that indicates whether this instance precedes, follows, or
appears in the same position in the sort order as the specified object
or String.
string.Equals:
Determines whether two String objects have the same value.
In short, CompareTo
is used for sorting. Equals
is used to determine equality.
Difference in comparing string .c_str() and normal string
std::get<1>(*it)
returns an object of type std::string. This class has overload operator ==
to compare objects of type std::string with character arrays.
std::get<1>(*it).c_str()
returns a character array. Arrays have no the comparison operator. To compare character arrays you should use standard C function std::strcmp
So you could write
if( std::strcmp( std::get<1>(*it).c_str(), "PAUSE" ) == 0 )
If you will write simply as
if(std::get<1>(*it).c_str()=="PAUSE")
then the compiler will compare two pointers because it converts arrays to pointers to their first elements in such expressions. And as the result this expression will be equal always to false if the arrays occupy different areas of memory.
Compare string c and character difference
First of all, no need to do any manipulation on strings, if the a character differ, another check to see if its because of upper or lower case is done.
Second of all, if string size range is 10-1000
, you need an extra character for the \0
in case a string of size 1000 is tested.
#include <stdio.h>
#include<string.h>
int main()
{
char str1[1001];
char str2[1001];
int iter = 0;
int i = 0;
int diff=0;
int counter = 0;
int len1=0;
int len2=0;
scanf("%s", str1);
scanf("%s", str2);
len1 = strlen(str1);
len2 = strlen(str2);
iter = (len1 > len2)?len2:len1;
for (i=0; i<iter; i++)
{
if (tolower(str1[i]) != tolower(str2[i]))
{
counter++;
}
}
diff = (len1 > len2)?len1:len2;
counter += diff-iter;
printf("%d\n", counter);
}
Input:
LEON
LeON
Output: 0
Input:
LEON
LION
Output: 1
Input:
LEONEE
LION
Output: 3
C string comparison
Because in C you have to use strcmp
for string comparison.
In C a string is a sequence of characters that ends with the '\0'
-terminating byte, whose value is 0.
The string "exit"
looks like this in memory:
+-----+-----+-----+-----+------+
| 'e' | 'x' | 'i' | 't' | '\0' |
+-----+-----+-----+-----+------+
where 'e' == 101, 'x' == 120, etc.
The values of the characters are determined by the codes of the ASCII Table.
&command != "exit"
is just comparing pointers.
while(strcmp(command, "exit") != 0);
would be correct. strcmp
returns 0 when both strings are equal, a non-zero
value otherwise. See
man strcmp
#include <string.h>
int strcmp(const char *s1, const char *s2);
DESCRIPTION
The
strcmp()
function compares the two stringss1
ands2
. It returns an integer less than, equal to, or greater than zero ifs1
is
found, respectively, to be less than, to match, or be greater thans2
.
But you've made another error:
scanf("%c", &command);
Here you are reading 1 character only, this command
is not a string.
scanf("%s", command);
would be correct.
The next error would be
char command[4];
This can hold strings with a maximal length of 3 characters, so "exit"
doesn't
fit in the buffer.
Make it
char command[1024];
Then you can store a string with max. length of 1023 bytes.
In general, of want to save a string of length n
, you need a char
array of
at least n+1
dimension.
Can I use != and == in C++ for string comparison without writing my own?
"Does it mean C++ deal with string (char []
) naturally with ==
and !=
?"
No. It is wrong to compare C-style strings (i.e. char[]
or char*
) between each other with ==
or !=
(in both C and C++), because you will end up comparing pointers rather than strings.
In C++ it is possible to compare std::string
object with a C-style string or another std::string
object because of the std::string
's operator==
and operator!=
:
std::string str1("abc"), str2("def");
if (str1 != str2 && str1 == "abc")
std::cout << "Yep, I got it.";
Related Topics
Fixed-Size Floating Point Types
Is Floating-Point Addition and Multiplication Associative
Serializing a Class Which Contains a Std::String
Overloading Member Access Operators -≫, .*
Using Char* as a Key in Std::Map
Using a C++ Class Member Function as a C Callback Function
Why Do C and C++ Support Memberwise Assignment of Arrays Within Structs, But Not Generally
Replace Substring With Another Substring C++
Problem Sorting Using Member Function as Comparator
What Are the Gcc Default Include Directories
Displaying the #Include Hierarchy For a C++ File in Visual Studio