String Manipulation Problems in Java -


I have asked 2 problems yesterday in an interview

1> Given a string, calculate a new string where the corresponding characters in the original letter are different from each other by a "*"

The example is as shown below: :: Function Name public string pair starter string

  pairStar ("hello") A ???? "Hello lo couple" ("xx") ??? "X * xy * y" partner ("AAAA") ??? "A * a * a * a"   

2> Given a string, calculate a new string where all lowercase 'x' characters have been moved to the end of the string. Example: The name of the function: public string endx (string str)

  endX ("xxre" ) a???? "Rex" end X ("xxhixx") ??? "Hixxxx" AndX ("Xhiksha") ??? "Hihixxx"   

I'm not sure how to complete the given set of problems and how it is struggling to solve.

for 1), here is a very simple rezox path:

  string in = "hello goodbye"; String out = in.replaceAll ("(.) (? = \\ 1)", "$ 1 *"); Println (outside);   

Print:

  hello * l go to * odbye   

explanation:

  (.) // matches any one letter for group 1 (? = \\ 1), that character for 1 $ 1 * / Code>  

I later 2) but I do not like many questions wrapped in one.

Edit

OK because I'm here in a reggae mood 2):

  string in = "xhix"; String out = in; While (! Out.matches ("[^ x] * x *")) {out = out.replaceAll ("x (. *)", "$ 1x"); } System.out.println (outside); It   

changes with x (some) to (some) x until all x I did not end the string, though I'm sure that with regex / there is a better way of doing this one.

Comments