วันอาทิตย์ที่ 4 ตุลาคม พ.ศ. 2558

Lab5-My replace

def my_replace(word,old,new):
   i_word = 0
   i_Alphabet = 0
   newWord = ''
   if(len(old)<len(word)):
      while(i_word<len(word)):
         if(cheakWord(i_word,i_Alphabet,old,word)):
            newWord += new[i_Alphabet]
            i_Alphabet += 1
         else:
            newWord += word[i_word]
         i_word += 1
      return newWord
 
def cheakWord(i_word,i_Alphabet,old,word):
   if(i_Alphabet<len(old)):
      if(word[i_word] == old[i_Alphabet]):
         return True
   return False

def setup():
   S='DevilMayCry'
   old='May'
   new='Die'
   print(my_replace(S,old,new))
 
 
setup()

Result
DevilDieCry

ไม่มีความคิดเห็น:

แสดงความคิดเห็น