Generation

generate functionSun, 05 Feb 2023

A word counter that excludes spaces as optimally as possible

def word_counter(string): count = 0 for i in range(len(string)): if string[i] == ' ': if string[i-1] != ' ' and string[i+1] != ' ': count += 1 elif i == 0: count += 1 return count

Questions about programming?Chat with your personal AI assistant