将字符串替换为特定格式的字符串程序

时间:2016-06-13 14:47:29

标签: string

WAP将字符串“MARY JEORGE WOODS”全名转换为'WOODS m。 J“。作为输出 即名字第一名和中名第一个字母只有全姓

我的程序没有运行并且给出了预期的输出。

2 个答案:

答案 0 :(得分:1)

full_name="MARY JEORGE WOODS"
list_name=full_name.split()
if list_name[2]: res=list_name[2]+' '+ list_name[0][0].lower()+'.'+list_name[1][0].lower()+'.'
else :
    res=list_name[1]+' '+ list_name[0][0].lower()+'.'

答案 1 :(得分:0)

full_name="MARY JEORGE WOODS"
list_name=full_name.split()
if(list_name.Length == 3)
    res=list_name[2]+' '+ list_name[0][0].lower()+'.'+list_name[1][0].lower()+'.'
if(list_name.Length == 2)
    res=list_name[1]+' '+list_name[1][0].lower()+'.'
if(list_name.Length == 1)
    res=list_name[0]
else
    res = "";