How it works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How it works?

def count_char(text, char): count = 0 for c in text: if c == char: count += 1 return count

5th Nov 2019, 4:11 PM
Preethi Loganathan
Preethi Loganathan - avatar
5 Answers
+ 5
Avinesh, you have given the perfect answer! (count_char() is a function) You should python give a try! Really well done!
5th Nov 2019, 5:40 PM
Lothar
Lothar - avatar
+ 2
I don't know Python but would still try according to the logic. Count_char is a method which accepts a string which is text and a character which is char. Count variable is initialized to 0. For loop iterates over one character of a string at a time then the if condition checks whether the character in string is equal to the character char or not. If it is equal than count is incremented by 1. This happens for every character in the string and the count value is returned.
5th Nov 2019, 4:35 PM
Avinesh
Avinesh - avatar
+ 1
Lothar appreciate your comments, thank you. By the way I'm learning java now and want to focus on that alone. But again thanks.
5th Nov 2019, 6:02 PM
Avinesh
Avinesh - avatar
+ 1
Thanks Avinesh
5th Nov 2019, 6:16 PM
Preethi Loganathan
Preethi Loganathan - avatar
+ 1
Preethi Loganathan your welcome
5th Nov 2019, 6:25 PM
Avinesh
Avinesh - avatar