I have a problem on which I am working where I need to count the number of words in a string without using the split()
function in Python.
I thought of an approach where I can take a variable word=0
and increment it every time there's an empty space in the string, but it doesn't seems to work as it always gave a count less than the actual count.
s="the sky is blue"
def countW(s):
print(s)
word=0
for i in s:
if i==" ":
word=word+1
print(word)
countW(s)
I know it's a simple question but I am struggling to understand what else I can keep into account to make sure I get the right count. The second approach I was thinking of involves too much for loop and array creation and then back string conversion. Can anyone point me to a simpler approach, where I don't increase the time complexity for this.