Below mentioned code is for reversing k elements in a string of n size. Line 3 is returning garbage value. Can anyone please help me out on this.
class Solution{
     public String reverseStr(String s, int k){
        char[] ch = s.toCharArray();
        Stack<Character> st = new Stack();
         int i;
        for(i = 0; i < k; i++){
            st.push(ch[i]);
        }
         i = 0;
         while(!st.isEmpty()){
             ch[i] = st.pop();
         }
         return ch.toString();
     }
}
 
     
    