Metal.days
Metal.days

Reputation: 85

Python - find substring in a string (code not working)

I'm writing a code, where you input a string (i) and a substring (sb) and the code should count the amount of times the substring appears in the string, with overlaps.

The code works if you input string "AAAA" and look for "A" ((returns the correct amount, 4) but if you put in "ADAM" and look for "A" it gets stuck in an infinite loop.

For the life of me, I cant solve this problem.

i = input("Enter a string:")
sb = input("Enter a substring:")

count = 0
x = 0    #index from the string found
idxTotal = len(i)  

while True:

    i.find(sb,x)

    if x != idxTotal:
        count += 1
        x = i.find(sb,x)+1

    else:
        break

print(count)

Upvotes: 1

Views: 1410

Answers (4)

devssh
devssh

Reputation: 1186

Can you try,

some_string = input("Enter a string:")
some_substring = input("Enter a substring:")
total = 0
for index, some_char in enumerate(some_string):
  # print(index, some_char) # try this to see what comes in the loop
  if some_string[index:].startswith(some_substring):
    total = total + 1

print(total)

It is more elegant, avoid using while(True) also i.find will return -1 therefore you will be stuck in infinite loop. This will allow you to avoid manually indexing with while. :)

This will work for "ADAM" and "A" with correct 2 and also for other substrings like "DABCEABC" and "ABC" with correct 2, "AAADAAAAAM" and "AAA" with the correct 5.

Upvotes: 0

Vijay Lingam
Vijay Lingam

Reputation: 101

Below code will work as per your needs:

i.count(sb,0,len(i))

Upvotes: -4

Pomonoli
Pomonoli

Reputation: 41

Since the find method returns -1 when no substring is found, I would use that to end your while loop.

By doing the following you count until the substring isn't found anymore:

i = input("Enter a string:")
sb = input("Enter a substring:")

count = 0
x = 0  

while True:
    x = i.find(sb,x)+1  #x will be 0 if sb isn't found
    if x != 0:          #if x = 0 the while loop will end
        count += 1
    else:
        break

print(count)

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476503

I think you make things too complicated. Basically you should check in the while loop that we did not reach the end of the string. Furthermore you should guarantee progress, by incrementing the offset value.

So we can write it like:

x = i.find(sb)
n = 0
while x >= 0:
    n += 1
    x = i.find(sb, x+1)
# here n is the number of occurrences
print(n)

So first we perform a i.find(sb) to find the first occurrence, and we set n (the count) to zero, each time x >= 0, we found a next occurrence, so then we increment n, and then we look for the next occurrence.

We keep doing that until .find(..) returns -1. In that case the while loop will stop, and n will contain the number of elements.

For example:

>>> i = 'ADAM'
>>> sb = 'A'
>>> x = i.find(sb)
>>> n = 0
>>> while x >= 0:
...     n += 1
...     x = i.find(sb, x+1)
...
>>> print(n)
2

This also performs overlap counts, like:

>>> i = 'AAADAAAAAM'
>>> sb = 'AAA'
>>> x = i.find(sb)
>>> n = 0
>>> while x >= 0:
...     n += 1
...     x = i.find(sb, x+1)
...
>>> print(n)
4 

So here for 'AAADAAAAAM' there are four matches with 'AAA':

  AAADAAAAAM
1 AAA
2     AAA
3      AAA
4       AAA

Upvotes: 5

Related Questions