0

Can someone help me why is it saying: "IndexError: string index out of range" When I add the "letterCount += 1" to the first else it makes this error, without it is working.

The goal is to count "bob"s in s.

Thanks!

s = 'oobobodobooobobobobabobbobbobobbobbobhbxbobbk'

vowelCount = 0
letterCount = 0
pointer = s

for pointer in s:  
    print(pointer)
    if pointer == 'b':
        print (str(letterCount) + '. betű B' )

        if (s[letterCount+1] + s[letterCount+2]) == str('ob') :
           vowelCount += 1
           letterCount += 1
           print( str(vowelCount) + '. BOB megtalálva')
        else:
           print('Nem OB jön utána')
           letterCount += 1
    else:      
        print(str(letterCount) + '. betű nem B')
        letterCount += 1

print ("Number of times bob occurs is: " + str(vowelCount))
  • 2
    You are not checking if (letterCount + 1) is greater than len(s) so you will likely reach the end of the array and get an index error when you get the latest element plus one – lapinkoira Sep 06 '16 at 08:54
  • `s.count("bob")`, for non-overlapping occurrences, http://stackoverflow.com/a/2970542/2681632 for overlapping. Also for future reference, use `for letterCount, pointer in enumerate(s):` if you need the index (instead of manual increments). – Ilja Everilä Sep 06 '16 at 09:07
  • Thank you very much, that was the problem! – Kovács Ádám Tamás Sep 06 '16 at 10:49

3 Answers3

0

You need to check the string s length with something like:

letterCount+2 <= len(s) 

i.e.

s = 'oobobodobooobobobobabobbobbobobbobbobhbxbobbk'

vowelCount = 0
letterCount = 0
pointer = s

for pointer in s:  
    print(pointer)
    if pointer == 'b':
        print (str(letterCount) + '. betű B' )

        if (letterCount+2 <= len(s) and (s[letterCount+1] + s[letterCount+2]) == str('ob')) :
           vowelCount += 1
           letterCount += 1
           print( str(vowelCount) + '. BOB megtalálva')
        else:
           print('Nem OB jön utána')
           letterCount += 1
    else:      
        print(str(letterCount) + '. betű nem B')
        letterCount += 1

print ("Number of times bob occurs is: " + str(vowelCount))
lapinkoira
  • 8,320
  • 9
  • 51
  • 94
0

I hope the following code will work for you.

s = 'oobobodobooobobobobabobbobbobobbobbobhbxbobbk'

vowelCount = 0

letterCount = 0

pointer = s

print len(s)

for pointer in s:

    if pointer == 'b':

        if (len(s) != letterCount+1 and len(s) != letterCount+2):

            if (s[letterCount+1] + s[letterCount+2]) == str('ob'):

                vowelCount += 1

                letterCount += 1

                print(str(vowelCount) + '. BOB')

            else:

                letterCount += 1
    else:

        letterCount += 1


print ("Number of times bob occurs is: " + str(vowelCount))

In this statement, i'm checking with letter count with the len of the string. it will match at the end of the string only.

or you can use the enumerator to check the len of the word in a string

for i, _ in enumerate(s): #i here is the index, equal to "i in 
range(len(s))"

if s[i:i+3] == 'bob': #Check the current char + the next three chars.

    bob += 1

print('Number of times bob occurs is: ' + str(bob))
Smittey
  • 2,475
  • 10
  • 28
  • 35
0
The final solution.

s = 'obbobbbocbobbogboobm'

vowelCount = 0
letterCount = 0
pointer = s

for pointer in s:  
    print(pointer)
    if pointer == 'b':
        print (str(letterCount) + '. betű B' )

        if (len(s)-2 > letterCount):      
            print('van utána két betű')
            if (s[letterCount+1] + s[letterCount+2]) == str('ob') :
               vowelCount += 1
               letterCount += 1
               print( str(vowelCount) + '. BOB megtalálva')
            else:
               print('Nem OB jön utána')
               letterCount += 1
        else:
           print('nincs utána két betű')           
           break
    else:      
        print(str(letterCount) + '. betű nem B')
        letterCount += 1

print ("Number of times bob occurs is: " + str(vowelCount))