9 Replies
The for loop in question is the last one, the sentence2[i] == sentence2[i++] one. When I remove it, the program works completely fine. With it, it suddenly does not print the last writeline saying it is correct even if it is
GFEDCBA is a good testing string as it should theoretically satisfy all the requirements
The for loop is meant to check if there are any repeating characters. My logic is that, once I sort the array, if there are any repeating characters, they will be next to each other, and so the IF should turn the NOTREPEATING boolean into false
I think the root of my issue is the ++, I should put a + 1 instead, I'll check when I get home
So for your example string:
GFEDCBAlength = 7 So in your loop:
i
will go up to 6, what happens then, what will sentence[i]
and sentence[i++]
be?There is no value 1 above the last
Hmmm
Thank you, I will check this when I get home
I am not sure how to fix that yet but I'll figure it out
What if I add another condition to the if, stating that i must not be equal to the length?
So it will stop once i = length which would be fine as position length was already checked with length - 1
Well
i
shouldn't reach length
anyway, because your for loop already has the condition i < length
, the problem is that when i
reaches its maximum value (length - 1
) sentence[i+1]
tries to access an index that doesn't existSo doesn't my change fix that as it prohibits i from reaching the maximum value?
no, because if
i
== length - 1
then it will still satisfy the condition i != length
but sentence2[i+1]
is still trying to access an index that doesn't exist
your for loop is letting i
increase to too high a value
i.e. if you've reached the last character, there's no next character to compare it to, so your for loop should stop before it reaches the last characterIn that case, is the solution to - 1 from length in the for loop?
So,
and the rest the same as at the start
You forgot your change of:
sentence2[i++]
to:
sentence2[i+1]
Oh yeah that too
Ty