if you want to remove an article from website contact us from top.

    in this challenge, the user enters a string and a substring. you have to print the number of times that the substring occurs in the given string. string traversal will take place from left to right, not from right to left.

    Mohammed

    Guys, does anyone know the answer?

    get in this challenge, the user enters a string and a substring. you have to print the number of times that the substring occurs in the given string. string traversal will take place from left to right, not from right to left. from screen.

    Hackerrank/Find_a_string.py at master · anarayanan86/Hackerrank · GitHub

    Solutions to the practice exercises, coding challenges, and other problems on Hackerrank! www.Hackerrank.com - Hackerrank/Find_a_string.py at master · anarayanan86/Hackerrank

    master

    Hackerrank/Python/Strings/Find_a_string.py / Jump to

    anarayanan86 Create Find_a_string.py

    1 contributor

    26 lines (19 sloc) 879 Bytes

    # Find a string

    # In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given

    # string. String traversal will take place from left to right, not from right to left.

    # NOTE: String letters are case-sensitive.

    # Input Format

    # The first line of input contains the original string. The next line contains the substring.

    # Constraints

    # 1 <= len(string) <= 200

    # Each character in the string is an ascii character.

    # Output Format

    # Output the integer number indicating the total number of occurrences of the substring in the original string.

    # Enter your code here. Read input from STDIN. Print output to STDOUT

    string = raw_input()

    substring = raw_input()

    count = 0

    for i in range(len(string) - len(substring) + 1):

    if string[i:i+len(substring)] == substring:

    count += 1 print count

    स्रोत : github.com

    to print the number of times that the substring occurs in the given string from left to right in python

    In this challenge, the user enters a string and a substring. User has to print the number of times that the substring occurs in the given string. String traversal will take place from left to right...

    to print the number of times that the substring occurs in the given string from left to right in python

    Ask Question

    Asked 2 years, 10 months ago

    Modified 4 months ago

    Viewed 8k times 0

    In this challenge, the user enters a string and a substring. User has to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.

    Example: Input: WoW!ItSCoOWoWW oW Output: 2 Code:

    def count_substring(string, sub_string):

    j=0 flag=0 counter=0

    for i in range(len(string)):

    if string[i] == sub_string[j]:

    if(j==0): flag=i j+=1 else: j=0

    if(j==len(sub_string)-1):

    counter+=1 i=flag+1 j=0 return counter

    if __name__ == '__main__':

    string = input().strip()

    sub_string = input().strip()

    count = count_substring(string, sub_string)

    print(count) Error:

    Traceback (most recent call last): File "C:/Users/shivangi/Documents/python codes/hackerrank solutions.py", line 24, in count = count_substring(string, sub_string) File "C:/Users/shivangi/Documents/python codes/hackerrank solutions.py", line 7, in count_substring if string[i] == sub_string[j]: IndexError: string index out of range

    I do not understand why it is showing "string index out of range" and how can it be solved

    pythonpython-3.xstring

    Share

    Improve this question

    edited Feb 13, 2020 at 7:56

    S.I.J 9711 1 gold badge 10 10 silver badges 22 22 bronze badges

    asked Feb 13, 2020 at 6:08

    Shivangi 952 2 silver badges 7 7 bronze badges Add a comment

    13 Answers

    6

    You can simply do next:

    input_str = 'WoW!ItSCoOWoWW'

    pattern = 'oW'

    print(input_str.count(pattern)) # will output 2

    And you don't need so complicated solution

    Also same with using regexp:

    import re

    input_str = 'WoW!ItSCoOWoWW'

    pattern = 'oW'

    print(len(re.findall(pattern, input_str))) # also output 2

    and simple for loop implementation

    idx = 0 pattern = 'oW'

    input_str = 'WoW!ItSCoOWoWW'

    pattern_len = len(pattern)

    counter = 0

    for ch in input_str:

    if ch == pattern[idx]:

    idx += 1 else: idx = 0

    if pattern_len == idx:

    counter += 1 idx = 0 print(counter) # 2 Share Improve this answer

    edited Feb 13, 2020 at 8:52

    answered Feb 13, 2020 at 6:14

    alex2007v 1,1907 7 silver badges 11 11 bronze badges Add a comment 1

    You are getting an index out of bounds because of j. So let's say the string is WowWowWow and the sub_string is ow. So when you are in the 4. loop, your i = 3 so you already checked Wow and you found one instance of the sub_string. But in that case, your j=2 because it got increased by one when you found the first o and then again when you found the first lower case w. But Your sub_string has no value at index [2]. So your sub_string[j] is then out of bounds. I hope everything is clear.

    A better solution would be to use

    counter = string.count(sub_string)

    print(counter) or

    print(string.count(sub_string))

    Share Improve this answer

    answered Feb 13, 2020 at 6:25

    sxeros 6735 5 silver badges 21 21 bronze badges Add a comment 0

    You can also split the string with the substring. It will return a list with n + 1 elements. For example, if substring occurs 2 times, then the length of list will be 3. So we need to reduce 1 from that list.

    s = 'WoW!ItSCoOWoWW'

    count = len(s.split('oW')) - 1 # output 2

    checked for this example, works fine. Not sure if it works for other example as well.

    Share Improve this answer

    answered Feb 13, 2020 at 6:26

    Nalin Dobhal 2,2122 2 gold badges 9 9 silver badges 19 19 bronze badges Add a comment 0

    While searching for substrings please make sure that it counts overlaps.

    Eg- string: rabdadada substring: dada Count should come two not one

    def countSubstr(string, sub_string):

    count = start = 0 while True:

    start = string.find(sub_string, start) + 1

    if start > 0: count+=1 else: return count

    if __name__ == '__main__':

    string = input().strip()

    sub_string = input().strip()

    count = countSubstr(string, sub_string)

    print(count) Share Improve this answer

    answered Mar 6, 2020 at 19:06

    Nikhil chaudhary 11 1 bronze badge Add a comment 0

    def count_substring(string, sub_string):

    c = 0

    for i in range(len(string)):

    if string[i:].startswith(sub_string):

    c = c + 1 return c

    if __name__ == '__main__':

    string = input().strip()

    sub_string = input().strip()

    count = count_substring(string, sub_string)

    print(count) Share Improve this answer

    answered Jul 6, 2020 at 7:28

    Apurv Mishra 11 1 bronze badge Add a comment 0

    Code 1

    def count_substring(string, sub_string):

    count = 0

    for i in range(len(string)):

    if string[i:].startswith(sub_string):

    स्रोत : stackoverflow.com

    Find a string in Python

    Find a string in Python - HackerRank Solution. In this challenge, the user enters a string and a substring. You have to print the number of times tha

    Find a string in Python - HackerRank Solution

    CodeWorld19 9 Apr, 2021 10 Input Format : Constraints : Output Format : Sample Input : Sample Output : Concept : Solution :

    Find a string in Python - HackerRank Solution

    Problem :

    In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.

    NOTE: String letters are case-sensitive.

    Input Format :

    The first line of input contains the original string. The next line contains the substring.

    Constraints :

    1 <= len(string)<= 200

    Each character in the string is an character.

    Output Format :

    Output the integer number indicating the total number of occurrences of the substring in the original string.

    Sample Input :

    ABCDCDC CDC

    Sample Output :

    2

    Concept :

    Some string processing examples, such as these, might be useful.

    There are a couple of new concepts:

    In Python, the length of a string is found by the function len(s), where is the string.

    To traverse through the length of a string, use a loop:

    for i in range(0, len(s)):

    print (s[i])

    A range function is used to loop over some length:

    range (0, 5)

    Here, the range loops over 0to 4. 5 is excluded.

    Solution :

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    # Find a string in Python - HackerRank Solution

    def count_substring(string, sub_string):

    # Find a string in Python - Hacker Rank Solution START

    count = 0

    for i in range(len(string)-len(sub_string)+1):

    if (string[i:i+len(sub_string)] == sub_string):

    count += 1 return count

    # Find a string in Python - HackerRank Solution END

    if __name__ == '__main__':

    string = raw_input().strip()

    sub_string = raw_input().strip()

    count = count_substring(string, sub_string)

    print count

    Disclaimer :

    the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form .

    स्रोत : codeworld19.blogspot.com

    Do you want to see answer or more ?
    Mohammed 1 month ago
    4

    Guys, does anyone know the answer?

    Click For Answer