DAILY CHALLENGE 12-03-18

String S is passed as the input to the program. S may or may not have a single underscore embedded in it. The program must reverse the String S till the first underscore and print it as the output. 

Input Format: The first line contains S. 

Output Format: The first line contains the string S modified based on the given conditions. 

Boundary Conditions: Length of S is from 3 to 100. 

Example Input/Output 1: 
Input: 
abcd_pqrs 

Output: 
dcba_pqrs 

Example Input/Output 2: 
Input: 
_kilo 

Output:
_kilo 

Example Input/Output 3: 
Input:
nounderscore 

Output: 
erocsrednuon

Python3 code
a=input().split('_')
b=[]
for i in a[0]:
    b.append(i)
b.reverse()
for i in b:
    print(i,end='')
if(len(a)==2):
    print('_',end='')
    print(a[1])




Java Code:
import java.util.*;
public class Hello {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        String s1=s.nextLine();
        int u=s1.indexOf('_');
        if(u==-1)
        System.out.print(new StringBuffer(s1).reverse().toString());
        else if(u==0)
        System.out.print(s1);
        else
        {
           if(u==s1.length()-1)
           {
               String[] s2=s1.split("_");
               System.out.print(new StringBuilder(s2[0]).reverse().toString()+"_");
           }
           else
           {
               String[] s2=s1.split("_");
               System.out.print(new StringBuilder(s2[0]).reverse().toString()+"_"+s2[1]);
           }
        }
    }
}

Post a Comment

0 Comments