Your task is to return how many times a string contains a given character.


The function takes a string(inputS) as a paremeter and a char(charS) which is the character that you will have to find and count.

For example, if you get an input string "Hello world" and the character to find is "o", return 2.

Difficulty: introductory

Starter code:

def string_counter(string, char):
	

Solutions:
def string_counter(string, char):
    return string.count(char)

string_counter = str.count    

def string_counter(string, char):
    return sum(1 for i in string if char == i)

def string_counter(strng, char):
    '''Return the frequency of a character in a string'''
    return strng.count(char)

string_counter = lambda string, char: string.count(char)

string_counter = lambda s,c: s.count(c)

def string_counter(string, char):
    count=0
    for i in range(0, len(string)):
        if string[i]==char:
            count=count+1
        else:
            pass

    return count


def string_counter(string, char):
    if char not in string: #stopping condition
        return 0
    c = string[0]
    if c == char:
        return 1 + string_counter(string[1:], char) #
    return string_counter(string[1:], char)

print((string_counter('Hello World', 'o')))


def string_counter(string, char):
    num=0
    for i in range(len(string)):
        if string[i]==char:
            num+=1
    return num

def string_counter(string, char):
    count=0
    for i in string:
        if i == char:
            count+=1

        else:
            pass

    return count