I am trying to figure out how to compare a character in a string with the next character in a string. For example, if I have a line:
s = 'vzcbotdebobeggglakyl'
I want to be able to compare the first character with the second character, and if the second character is greater than or equal to the first character (in alphabetical order, <b, g> e, y = y, etc.) I want to add 1 to another variable (basically a counter). If it is not, I want to reset the counter to 0. And basically repeat the whole process for the length of the string. If the counter becomes larger than the maxlen variable, add it to maxlen (or make maxlen = sublen). My attempt so far (and I think it works):
s = 'vzcbotdebobeggglakyl'
sublen = 1
maxlen = 0
startnum = 0
for char in s:
stopnum = startnum + 1
if stopnum < len(s):
charone = s[startnum]
chartwo = s[stopnum]
if charone <= chartwo:
sublen += 1
startnum += 1
if sublen > maxlen:
maxlen = sublen
else:
startnum +=1
sublen = 1
else:
sublen = 0
print 'Longest substring is', maxlen, 'characters.'
, , . . , , . ,
Longest substring is begggl, which is 6 characters.