Maria F. answered 05/16/26
Python Tutor | Python Basics, Debugging, OOP & Assignments Help
def count(s, ch):
c = 0
for i in s:
if i == ch:
c += 1
return c
s = input(“Enter a string: “)
ch = input(“Enter a character: “)
print(count(s, ch))
Richard P.
asked 04/12/24Write a function that finds the number of occurrences of a specified character in the string using the following header: def count(s, ch): For example, count("Welcome", 'e') returns 2. The str class has the count method. Implement your function without using the count method. Write a test program that reads a string and a character and displays the number of occurrences of the character in the string.
Maria F. answered 05/16/26
Python Tutor | Python Basics, Debugging, OOP & Assignments Help
def count(s, ch):
c = 0
for i in s:
if i == ch:
c += 1
return c
s = input(“Enter a string: “)
ch = input(“Enter a character: “)
print(count(s, ch))
Inactive Tutor answered 04/20/24
def count(s, ch):
count = 0
for char in s:
if char == ch:
count += 1
return count
# Test program
def main():
# Read input string and character
input_string = input("Enter a string: ")
input_char = input("Enter a character: ")
# Count occurrences of the character in the string
occurrences = count(input_string, input_char)
# Display the result
print(f"The character '{input_char}' occurs {occurrences} time(s) in the string.")
if __name__ == "__main__":
main()
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.