codextj

quick_replace

Oct 5th, 2018
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. s0 = "foobarbaz" # --> friendbarbaz
  2. s1 ='foofoofoo'  # --> friendfriendfriend
  3. s2 = 'foodoof goof foog' # --> frienddoof goof friendg
  4. s = 'f o o d' #--> f o o d
  5.  
  6.  
  7. tobereplace = 'foo'
  8. replacement= 'friend'
  9.  
  10.  
  11. new_s = ''
  12. i=0
  13. while i < len(s):
  14.     temp = ''
  15.     j = 0
  16.     while j< len(tobereplace) and s[i] == tobereplace[j]:
  17.         temp += s[i]
  18.         i+=1
  19.         j+=1
  20.    
  21.     if temp == tobereplace:
  22.         new_s += replacement
  23.     else:
  24.         new_s += temp+s[i] if temp != '' else s[i]
  25.         i+=1
  26.        
  27. print(new_s)
Add Comment
Please, Sign In to add comment