19 Jan 2015

Count of Substring in String

#Python
'''
Count the number of times the substring 'bob' appears in string s

'''


s='azcbobobegghakl'
i=0
ocr=0
while(i<(len(s)-2)):
    ocr+=s.count('bob',i,i+3)
    i+=1      
print "Number of times bob occurs is:", ocr     



Categories: ,

0 comments:

Post a Comment

Copyright © UPgradeCODING | Powered by Blogger