Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote