Merge two strings letter by letter in Java?

前端 未结 7 1369
梦毁少年i
梦毁少年i 2021-01-21 22:05

Given two strings, A and B, create a bigger string made of the first char of A, the first char of B, the second char of A, the second char of B, and so on. Any le

7条回答
  •  长发绾君心
    2021-01-21 22:51

    I used Merge sort approach to solve this problem. First I converted both the strings into their respective character arrays and then merged both the arrays and converted the array back to a string. You can find my code below,I have tested the code and it is working. Let me know if you have any questions..

    public String merge(String leftStr, String rightStr) {  
            char[]left = leftStr.toCharArray();
            char[]right = rightStr.toCharArray();
            int nL = left.length;
            int nR= right.length;
            char[] mergeArr= new char[nL+nR];
            int i=0,j=0,k=0;
            int temp =0;
            while(i

提交回复
热议问题