Why aren't these two strings equivalent? C

mr_jay

I'm currently working my way through CS50x and just tinkering around with some of the practice problems from Week 1 and testing the limits of my knowledge. Could someone help me understand why the two strings "my_month" and "your_month" aren't equal?

I've tested it and if I print the string for "my_month" it prints "October" - but if I enter "October" for "your_month" it doesn't seem to think that it is equivalent to the string in the array.

string month [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
string my_month = month [9];
string your_month = get_string("What month were you born? ");

if (your_month == my_month)
{
    printf("%s\n", your_month);
}
SarcasticLeo

The reason is because technically they aren't equal. When you use '==' on strings, what you are comparing is their addresses.

To compare the contents of 2 string you have you have to use strcmp().

So your code should look like this:

if (strcmp(your_month, my_month) == 0)
{
    printf("%s\n", your_month);
}

strcmp() works by comparing each character's ASCII value. So it can also tell you if the first string is "more" or "less" than the second one.

For example:

strcmp("lol", "LOL");

Will return a value bigger than 0 which means the ASCII values of "lol" are higher than those of "LOL"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why aren't the two code blocks equivalent?

Golang Why aren't these two strings equal?

Why aren't those two expressions semantically equivalent?

Why aren't these strings equal

Why are strings considered tokens in C while arrays aren't?

Why aren't these 2 expressions equivalent in bash?

Why aren't these destructuring assignments equivalent?

Why aren't these two R objects identical?

Why aren't these two string equals?

Two methods with same erasure aren't necessary override-equivalent (or they signatures aren't subsignatures between them)?

Strings aren’t objects

Aren't Python strings immutable? Then why does a + " " + b work?

Aren't Python strings immutable? Then why does a + " " + b work?

Why aren't empty strings filtered out of this Java stream?

Why aren't identical arrays, when passed as strings considered equal?

Rcpp CharacterVector: why aren't individual elements std::strings?

Aren't a[][] and (*a)[] equivalent as function parameters?

Why aren't these two sql statements returning same output?

Why aren't the images of the first two instances of WhitePawn() displayed?

Why aren't these two result sets adding up?

Why those two xml string aren't similar

Why aren't two `zip` objects equal if the underlying data is equal?

Matlab: strings aren't really strings?

Why aren't include guards in c++ the default?

Why aren't c# programmers drawn to ruby as java programmers are

Why aren't my If and Else statements doing nothing? (c++)

Why aren't C++ Lambda Expressions Working?

C++ CUDA: Why aren't my block dimensions working?

Copy all rows between two strings that aren´t at the start of the row-python