""" This program: - asks the user to enter two strings, string1 and string2. - finds all positions in string1 where string2 occurs. - prints out those positions and the total number of such positions. Note: overlapping occurrences of string2 are NOT discarded. For example, if string1 = "abcIOIOIOIOIOabc" and string2 is "IOIOIO", the program will report that string2 appears 3 times in string1. """ string1 = raw_input("enter the first string: ") string2 = raw_input("enter the second string: ") # temp_string starts off equal to string1, and becomes a more and more # truncated version of string1. Every time we find string2 in temp_string, # we truncate the start of temp_string up to (and including) the first # character of the occurrence of string2. temp_string = string1 count = 0 positions = [] adjustment = 0 # go through the first string, looking for occurrences of the second string while True: # find next position position = temp_string.find(string2) if position == -1: break count = count + 1 positions.append(position + adjustment) # truncate the start of temp_string, to strip off the # occurrence of string2 that we just found temp_string = temp_string[position+1:] # adjustment remembers how many total characters we have truncated # from string1 to obtain the current version of temp_string # this is important so that we can convert the position of string2 # in temp_string into the corresponding position of string2 in string1. adjustment = adjustment + position + 1 print "The second string appears in the first string", count, "times." print "positions:", positions