Using Python write a function that returns number of occurrences of a particular
ID: 3926584 • Letter: U
Question
Using Python write a function that returns number of occurrences of a particular character in a string.
Do not use any built-in functions or methods such as count of str class. def countCharacter(s, char):
The function countCharacter receives a string, s and a character, char and returns the number of occurrences of this particular character.
Write a main function that:
• Reads a string and the character to be counted from the user
• Calls the function countCharacter
• Prints the count of characters
Explanation / Answer
def countCharacter(s,char):
count=0
for i in range(0,len(s)-1):
if(s[i]==char):
count=count+1;
return count
def main():
in=input('enter string:');
ch=input(' enter char :');
print 'no of occurrence of',ch,'is :',countCharacter(in,ch)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.