+ 2
How it works?
def count_char(text, char): count = 0 for c in text: if c == char: count += 1 return count
5 Antworten
+ 5
Avinesh, you have given the perfect answer! (count_char() is a function) You should python give a try! Really well done!
+ 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.
+ 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.
+ 1
Thanks Avinesh
+ 1
Preethi Loganathan your welcome