Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the first character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter. Example: Input text = "When the SUN rises at dawn, the chicken flies into the window." Input letter = "t" Output = "The letter t has appeared 3 times as the first letter of a word."
Python 3 programming
def firstLetterCount(line, letter): count = 0 for x in line.split(): if len(x)>0 and x[0]==letter: count += 1 return count # Testing text = "When the SUN rises at dawn, the chicken flies into the window." letter = "t" count = firstLetterCount(text,letter) print("The letter",letter,"has appeared",count,"times as the first letter of a word.")
Get Answers For Free
Most questions answered within 1 hours.