首页 > 试题广场 >

假设有一段英文,其中有单词中间的字母“i”误写为“I”,请编

[问答题]

假设有一段英文,其中有单词中间的字母“i”误写为“I”,请编写程序进行纠正。

不用正则就不行么……
x = "I am a teacher,I am man, and I am 38 years old.I am not a busInessman."
x = list(x)
for i in range(len(x)-1):
    if x[i] == 'I': 
        if x[i+1] !=' ':
            x[i] = 'i'
x = ''.join(x)
print(x)   



发表于 2022-06-09 18:25:26 回复(0)
import re
s = "adbjsdIsdbI hdjhI Ifdjidh " a = re.sub(r'\BI\B','i',s) print(a)

发表于 2020-04-15 17:39:35 回复(0)

这里给出Python 3.4.2代码,如果使用Python 2.7.8的话只需要修改其中的print()函数为print语句即可。

import re
x = "I am a teacher,I am man, and I am 38 years old.I am not a busInessman."
print(x)
pattern = re.compile(r'(?:[\w])I(?:[\w])')
while True:
    result = pattern.search(x)
    if result:
        if result.start(0) != 0:
            x = x[:result.start(0)+1]+'i'+x[result.end(0)-1:]
        else:
            x = x[:result.start(0)]+'i'+x[result.end(0)-1:]
    else:
        break
print(x)

发表于 2017-12-28 15:45:38 回复(1)