Find a string

Sort by

recency

|

3387 Discussions

|

  • + 0 comments

    def count_substring(string, sub_string): count = 0 start = 0

    while True:
        start = string.find(sub_string, start) 
        if start == -1:  
            break
        count += 1
        start += 1  
    return count
    

    if name == 'main': string = input().strip() sub_string = input().strip()

    count = count_substring(string, sub_string)
    print(count)
    
  • + 0 comments

    def count_substring(string, sub_string): count = 0 while sub_string in string : find = string.find(sub_string) string = string[find+1:] count += 1 return count

    if name == 'main': string = input().strip() sub_string = input().strip()

    count = count_substring(string, sub_string)
    print(count)
    
  • + 0 comments
    def count_substring(string, sub_string):
        count = 0
        
        for i in range(0,len(string)-len(sub_string)+1):
            if(string[i: len(sub_string)+i] == sub_string):
                count += 1
            
        return count
    
  • + 0 comments

    This is a great challenge for practicing string manipulation and pattern matching! Counting the occurrences of a substring within a string is a useful skill, and traversing the string from left to right, 11 winner

  • + 0 comments

    Ya'll answers are complicated. I still used it for reference and this is what I came up with.

    def count_substring(string, sub_string): counter = 0

    for i in range(1, len(string)):
        if sub_string in string:
            find = string.find(sub_string)
            string = string[find + 1:]
            counter += 1
    return counter
    

    if name == 'main': string = input().strip() sub_string = input().strip()

    count = count_substring(string, sub_string)
    print(count)