How do you count the number of words in the line?



  • It's natural number n and sequence of S1...Sn symbols. The symbol groups, divided by one or more gaps, will be called words. Count the words in this line.



  • The idea is simple, the number of words equals the number of gaps between words + 1. You need to take into account the related gaps and not respond to them.

    The approximate algorithm, the details and the implementation will be:

    • Going in the line cycle, watch every symbol.
    • Keep your previous symbol.
    • If you see the gap and the last symbol, you've got a little more words.
    • Remember last symbol
    • We've come to the end - if there's a gap at the end, you come out if the symbol increases. One more.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2