+ 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?
4 Antworten
+ 2
You are correct. Add an asterisk to both x and y to compare the first characters of both strings.
+ 1
Thanks!
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
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.