title: Python word counter
body: <p>I'm taking a Python 2.7 course at school, and they told us to create the following program:</p>
  <blockquote>
  <p>Assume s is a string of lower case characters.</p>
  <p>Write a program that prints the longest substring of s in which the letters occur in alphabetical order.</p>
  <p>For example, if s = azcbobobegghakl , then your program should print</p>
  <p>Longest substring in alphabetical order is: beggh</p>
  <p>In the case of ties, print the first substring.</p>
  <p>For example, if s = 'abcbcd', then your program should print</p>
  <p>Longest substring in alphabetical order is: abc</p>
  </blockquote>
  <p>I wrote the following code:</p>
  <pre><code>s = 'czqriqfsqteavw'
  
  string = ''
  
  tempIndex = 0
  prev = ''
  curr = ''
  
  index = 0
  while index &lt; len(s):
      curr = s[index]
      if index != 0:
          if curr &lt; prev:
              if len(s[tempIndex:index]) &gt; len(string):
                 string = s[tempIndex:index]
              tempIndex=index
          elif index == len(s)-1:
              if len(s[tempIndex:index]) &gt; len(string):
                 string = s[tempIndex:index+1]
      prev = curr
      index += 1
  
  print 'Longest substring in alphabetical order is: ' + string
  </code></pre>
  <p>The teacher also gave us a series of test strings to try:</p>
  <pre class="lang-none prettyprint-override"><code>onyixlsttpmylw  
  pdxukpsimdj  
  yamcrzwwgquqqrpdxmgltap  
  dkaimdoviquyazmojtex  
  abcdefghijklmnopqrstuvwxyz  
  evyeorezmslyn  
  msbprjtwwnb  
  laymsbkrprvyuaieitpwpurp  
  munifxzwieqbhaymkeol   
  lzasroxnpjqhmpr    
  evjeewybqpc   
  vzpdfwbbwxpxsdpfak    
  zyxwvutsrqponmlkjihgfedcba  
  vzpdfwbbwxpxsdpfak     
  jlgpiprth  
  czqriqfsqteavw 
  </code></pre>
  <p>All of them work fine, except the last one, which produces the following answer:</p>
  <blockquote>
  <p>Longest substring in alphabetical order is: cz</p>
  </blockquote>
  <p>But it should say:</p>
  <blockquote>
  <p>Longest substring in alphabetical order is: avw</p>
  </blockquote>
  <p>I've checked the code a thousand times, and found no mistake. Could you please help me?</p>