String underscore problem



Problem to reverse the string before the underscore only.



Example-1:


Input    : S = "ABC_XYZ"
Output   : CBA_XYZ  


Example-2:


Input    : S = "ABC"
Output   : CBA    


Example-3:


Input    : S = "ABC_"
Output   : CBA_  







Solution





n = "ABC_XYZ"

d = ""

d1 = ""

f = False

for i in n:

    if(i=="_"):

        break

    else:

        d += i

for j in n:

    if(j=="_"):

        f = True

    if(f):

        d1 += j

if("_" in n):

    a = d[::-1] + d1

    print(a)

else:

    b = d[::-1]

    print(b)



Output



[2, 4, 5, 3, 1]