10 Censor - what if I don't want to use .split function, in fact it doesn't work for situations like strings do not have blank space in between


#1



https://www.codecademy.com/zh/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096

what if I don't want to use .split function, in fact it doesn't work for situations like strings do not have blank space in between.

If so, .split won't work

please advise, how should I revise my code in below or how should i continue finishing it, thanks.
(I thought about this quesitons for 2 days in total, over the weekend :stuck_out_tongue:

def censor(text, word):
        l = len(text)
        w = len(word)
        for  i in range(l):
            if text[i] == word[0]:
                for j in range(w):
                    if text[i+1+j] == word[j+1]:
                        continue
            else:
                break

Share with all of you another two versions of censor practice, with use of .split

'''def censor(text, word):
    wordList = text.split()
    result = []
    print wordList
    for w in wordList:
        if w == word:
            result.append( "*"*len(word))
        else:
            result.append(w)
    return " ".join(result)'''
    
'''def censor(text, word):
    wordList = text.split()
    print wordList
    for w in wordList:
        if w == word:
            wordIndex = wordList.index(w)
            wordList[wordIndex] = "*"*len(word)
    return " ".join(wordList)'''


#2

Without use of .split() in text with or without white space:

def censor(text, word):
    ctxt = ""
    l = len(text)
    w = len(word)
    i = 0
    while i in range(l):
        if text[i:w+i] == word:
            ctxt += "*" * w
            i += w - 1
        else:
            ctxt += text[i]
        i += 1
    return ctxt
censor("themaddog","mad")      # 'the***dog'
censor("the mad dog","mad")    # 'the *** dog'