Skip to content

Instantly share code, notes, and snippets.

@saviour123
Created April 8, 2021 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saviour123/f66e15419078ca03cbe5f9a6786303b4 to your computer and use it in GitHub Desktop.
Save saviour123/f66e15419078ca03cbe5f9a6786303b4 to your computer and use it in GitHub Desktop.
Counter the Number of words in an arbitrary strings
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