Computer Science
Write a program to count the number of times a character occurs in the given string.
Python
Python String Manipulation
65 Likes
Answer
str = input("Enter the string: ")
ch = input("Enter the character to count: ");
c = str.count(ch)
print(ch, "occurs", c, "times")
Output
Enter the string: KnowledgeBoat
Enter the character to count: e
e occurs 2 times
Answered By
25 Likes
Related Questions
Write a program which reverses a string and stores the reversed string in a new string.
Write a program that prompts for a phone number of 10 digits and two dashes, with dashes after the area code and the next three numbers. For example, 017-555-1212 is a legal input. Display if the phone number entered is valid format or not and display if the phone number is valid or not (i.e., contains just the digits and dash at specific places.)
Write a program that should do the following :
- prompt the user for a string
- extract all the digits from the string
- If there are digits:
- sum the collected digits together
- print out the original string, the digits, the sum of the digits
- If there are no digits:
- print the original string and a message "has no digits"
Sample
- given the input : abc123
prints abc123 has the digits 123 which sum to 6 - given the input : abcd
prints abcd has no digits
Write a program which replaces all vowels in the string with '*'.