Char comparison | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Char comparison

I came across with this ///////////////////////////////// int main (){ char x [] = "bye" , y[] = "bye"; if (x!=y) cout<<"not" cout<<"equal"; return 0; } //////////////////////// Output notequal Why is the if conditional true? Is it because "x" and "y" are pointers which hold different addresses?

25th Jul 2019, 11:45 PM
Geral
4 Answers
+ 2
You are correct. Add an asterisk to both x and y to compare the first characters of both strings.
26th Jul 2019, 12:08 AM
Cluck'n'Coder
Cluck'n'Coder - avatar
+ 1
Thanks!
26th Jul 2019, 12:09 AM
Geral
0
To compare following chars in a string and if they're equal, raise the counter. With my example code I always get TypErrors related to line 6. Do you know where's the problem? def func(text): counter = 0 text_l = text.lower() for i in text_l: if text_l[i+1] == text_l[i]: print(text_l[i+1], text_l[i]) counter += 1 return counter
7th Sep 2019, 3:33 AM
Gwendolyn Phillips de Ashborough
Gwendolyn Phillips de Ashborough - avatar
0
@PoloLadyCoder - You are checking the current character with the next character, but what happens when you reach the last character of the string? -> You end up checking past the string length. Changing your for-loop condition to: for i in range(len(text_l)-1): - should solve the problem. If not, let me know.
9th Sep 2019, 7:20 AM
Cluck'n'Coder
Cluck'n'Coder - avatar