-
Notifications
You must be signed in to change notification settings - Fork 1
/
lintcode212.py
43 lines (39 loc) · 1 KB
/
lintcode212.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 21 15:10:40 2015
Write a method to replace all spaces in a string with %20. The string is given
in a characters array, you can assume it has enough space for replacement and
you are given the true length of the string.
Example
Given "Mr John Smith", length = 13.
The string after replacement should be "Mr%20John%20Smith".
@author: linla
"""
string = raw_input('Enter a string:\n')
try:
string = str(string)
except:
'it is not a string'
newStr = str()
lg = len(string)
if lg >0:
for i in range(0,lg):
if string[i]!=' ':
newStr = newStr + string[i]
else:
newStr = newStr + '%20'
continue
length = len(newStr)
print newStr
# using character array menthod
'''
oldList = list(string)
newList = []
for i in range(0, len(oldList)):
if oldList[i] == ' ':
newList.append('%20')
else:
newList.append(oldList[i])
newString = ''.join(newList)
print len(newString)
'''