How do I find and count all occurrences of a substring in a string using only find and replace?

The entry should be below, and at the end the program should print the number of occurrences. For example mem.

smthing = str(input())
if (smthing == smthing.lower()):
    smthing.find(mem)

      

I am completely deceived so I could not go far.

I forgot to say that I cannot use an account or a list.

+3


source to share


7 replies


Something like

string = "hello world hello".lower()
replace = "hello".lower()
count = 0

while string.find(replace) != -1:
    string = string.replace(replace, "", 1)
    count += 1

print count
# => Output
# => 2

      




To take care of overlapping strings instead of replacing the entire substring, we could just replace one character, preferably the first, replace[0]

from the original string

string = "boboobobobobob".lower()
replace = "bob".lower()
count = 0 

while string.find(replace) != -1:
    string = string.replace(replace[0], "", 1)
    count += 1

print count
# Output
# => 6

      

+4


source


If you have overlapping lines, you will need to replace the aa character by the time:

sub = "bob"
smthing = input()
count = 0

for i in iter(lambda: smthing.find(sub), -1):
    smthing = smthing.replace(sub[0], "", 1)
    count += 1
print(count)

      

So for boboobobobobob

you get 6 instead of 3.



If you cannot use the count, but can use one or the other, you can use the substitution yourself, although this will not include overlap:

print((len(smthing) - len(smthing.replace(sub,""))) / len(sub))

      

+2


source


Try this code:

smthing = "blablabla"
mem = "bl"
count = 0
if (smthing == smthing.lower()):
    bkpsmthing = smthing # maybe you'll need it later
    while (smthing.find(mem) > -1):
        smthing = smthing.replace(mem, '', 1)
        count += 1
print count

      

It takes advantage of the fact that it str.find

returns -1 when it finds nothing, or an index to the bottommost record to stop:

Return the lowest index in s where the substring sub is found, so that sub is entirely contained in s [start: end]. Return -1 on error.

It also takes advantage of the fact that it str.replace

can delete one entry (the lowest one) using the thrid ( maxreplace

) argument , so we are constantly deleting the entries we have counted:

... If the optional maxreplace argument is given, the first occurrences of maxreplace are replaced.

The process can be described as follows:

find "bl" in "blablabla" -> found at index 0
replace "bl" in "blablabla" -> gives "ablabla"
find "bl" in "ablabla" -> found at index 1
replace "bl" in "ablabla" -> gives "aabla"
find "bl" in "aabla" -> found at index 2
replace "bl" in "aabla" -> gives "aaa"
find "bl" in "aaa" -> not found, returns -1
stop

      

To do the same with a variable count

, use this simple recursion (amke is sure you are checking the lowercase string before using my_count

):

def my_count(my_string, my_substring):
    if my_string.find(my_substring) > -1:
        new_string = my_string.replace(my_substring, '', 1)
        return my_count(new_string, my_substring) + 1
    return 0

      

Output:

>>> my_count("blablabla", "bl")
3

      

The recursion unfolds as follows:

my_count("blablabla", "bl") =
= my_count("ablabla", "bl") + 1 =
= my_count("aabla", "bl") + 1 + 1 =
= my_count("aaa", "bl") + 1 + 1 + 1 =
= 0 + 1 + 1 + 1 = 3

      

0


source


You don't; you need additional tools, maybe just basic arithmetic. For example, if you replace your substring with another, the length of the result can be compared to the original to count the number of occurrences. Another option is to use the start argument to find additional occurrences. I'm curious to know what you have tried; the code you are showing just doesn't give any result.

0


source


For the record, you can do it with string.replace()

:

smthing = str(input())  
word='mem'
x=0

while word in smthing:
    smthing=smthing.replace(word,'',1)
    x+=1

print x

      


Demo:

>>> 
memory memory memory
3

      

0


source


Your problem - finding and counting all occurrences of a substring in a string - doesn't require replacement as defined.

You can just take your input, force it down and use the count method to find substrings.

num_substrings = input().lower().count(substring)

      

-1


source


I don't see this approach yet, just loop over the meet counts:

a='this is a test aaaa'
count =0
loc = a.find('is')
while loc > -1:
    count += 1
    loc = a.find('is',loc+1)
print count -1 

      

-1


source







All Articles