I get the text-string from the file. Afterwards I do a for
loop over the string (I have to save words) and all works fine except the last word in the file, if there's no separator after it.
My code:
for symbol in text:
if symbol == ' ' or symbol == '-' or symbol == ',' or symbol == '\n':
lastWord = ''.join(tmpList)
del tmpList[:]
print lastWord
else:
tmpList.append(symbol)
I've figured that there is no NULL-termination in Python. Maybe I'm trying to solve this in the C way, and such algorithm for Python is incorrect?
I've added count variable and one more check to "else" block and it works fine. I wonder if it's correct, or i can do the same easier in python. Else-block now looks like:
else:
tmpList.append(symbol)
count += 1
if count == len(text):
lastWord = ''.join(tmpList)
del tmpList[:]
print lastWord