Created
April 8, 2021 09:19
-
-
Save saviour123/f66e15419078ca03cbe5f9a6786303b4 to your computer and use it in GitHub Desktop.
Counter the Number of words in an arbitrary strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sentence = " this is a debug message" | |
def counter(str): | |
"this should be done with no standard library" | |
words = 0 | |
prevSpace = True | |
indx = 0 | |
for i in sentence: | |
if str[indx] != " " and prevSpace: | |
words += 1 | |
prevSpace = str[indx] == " " | |
indx += 1 | |
return words | |
# test cases | |
sentence1 = " this is a debug message" | |
sentence2 = "this is a debug message" | |
print(counter(sentence1)) | |
print(counter(sentence2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment