forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.py
31 lines (25 loc) · 778 Bytes
/
2.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
# -*- coding:utf-8 -*-
'''
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
'''
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
for index in range(len(s)):
if s[index]==' ':
s[index]='%20'
return s
def replaceSpace2(self, s):
# write code here
targetChar = ' '
returnS = ''
for itemChar in s:
if itemChar == targetChar:
returnS += '%20'
else:
returnS += itemChar
return returnS
s = 'We Are Happy.'
soulution = Solution()
print soulution.replaceSpace(s)