Company:
Microsoft, MakeMyTrip, Goldman-Sachs, Cisco, Amazon, Adobe , Accolite, SAP-Labs, Paytm
Question:
Example:
Input:
2
i.like.this.program.very.much
pqr.mno
Microsoft, MakeMyTrip, Goldman-Sachs, Cisco, Amazon, Adobe , Accolite, SAP-Labs, Paytm
Question:
Given a String of length N reverse the words in it. Words are separated by dots.
Input:
The first line contains T denoting the number of testcases. Then follows description of testcases. Each case contains a string containing spaces and characters.
The first line contains T denoting the number of testcases. Then follows description of testcases. Each case contains a string containing spaces and characters.
Output:
For each test case, output a single line containing the reversed String.
For each test case, output a single line containing the reversed String.
Constraints:
1<=T<=20
1<=Lenght of String<=2000
1<=T<=20
1<=Lenght of String<=2000
Example:
Input:
2
i.like.this.program.very.much
pqr.mno
Output:
much.very.program.this.like.i
mno.pqr
much.very.program.this.like.i
mno.pqr
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int j=0;j<m;j++){
String s= sc.next();
String[] strs = s.split("[.]");
for (int i=strs.length-1; i >= 0; i--) {
if(i!=0){
System.out.print(strs[i]+".");}
else{System.out.print(strs[i]);}
} System.out.println();
}
}
}
NOTE: Companies here look to know whether you have basic knowledge on core JAVA. Here one merely needs to implement string.split() method and reverse it. In case no "." is given in between words, just use string.split(" ") denoting string needs to be split based on space.
Comments
Post a Comment