Devendra Singh Devendra Singh - 5 months ago 12
Java Question

String concatenation as character array (without library method)

I want to store the value of 2 strings str1, str2 respectively into 3rd string strContainer (without using library method).

My Algorithm is:

1. convert str1 and str2 into character array charArray1 and charArray2 respectively.

2. Count the sum of the length of both character array in counter variable (sum of the length charArray and charArray2)

3. Sum of the both char Arrays (as counter) is equivalent to a new char Array charContainer.

4. Iterate a loop as below

charContainer[ith index] += charArray1[ith index];


and

charContainer[ith index] += charArray2[ith index];

5. Convert charContainer into string and display as strContainer.




My code so far:


public class StringConcat{
int counter; // counting the length of char arrays
String str1 = "FirstString";
String str2 = "SecondString";

//for counting the length of both char arrays
public int countingLength(String str){
char[] strToCharArray = str.toCharArray();
for(char temp : strToCharArray){
counter++;
}
}

//converting string into char array
char[] charArray1 = str1.tocharArray();
char[] charArray2 = str1.tocharArray();

//stores both char array
char[] charContainer=new char[counter];//how to take counter as an index value here

//for storing charArray1 into charContainer
for(int i=0; i<charContainer.length; i++) {

if(charArray1 != null){
charContainer[i] += charArray1[i];
} else
return charArray2;
}

//for storing charArray2 into charContainer
for(int i=0; i<charContainer.length; i++) {

if(charArray2 != null){
charContainer[i] += charArray1[i];
} else
return charArray1;
}

//converting charContainer char array into string strContainer.
String strContainer = new String(charContainer);
//alternative : String strContainer = String.valueOf(charContainer);

public static void main(String args[]){

/*Here i can call (As i'm not sure)
StringConcat obj1 = new StringConcat();
obj1.countingLength(str1);

StringConcat obj2 = new StringConcat();
obj2.countingLength(str2);
*/
System.out.println("String Container : " +strContainer);
}

}//end of the class


Issues:

How to call countingLength() method for both strings str1 and str2 ?

How to assign as an index value of charContainer as counter (sum of the both char arrays) ?

Answer

How to call StringLengthCounter() method? I can't see any method with that name.. I'm sorry but that is not the problem here, the problem is that this is not even valid code.

I don't mean to be harsh but there are sintax error all around and the program logic is wrong in many ways.

Please take a look at the following code and try to figure out how it works, I think it does what you want. If something isn't clear just ask.

public class StringConcat{
    public static String strcat(String str1, String str2){
        //converting string into char array
        char[] charArray1 = str1.toCharArray();
        char[] charArray2 = str2.toCharArray();

        int counter=charArray1.length+charArray2.length;

        //stores both char array   
        char[] charContainer=new char[counter];

        //for storing charArray1 into charContainer
        int i=0;
        for(; i<charArray1.length; i++) {
            charContainer[i]=charArray1[i];
        }
        //for storing charArray2 into charContainer
        for(int j=0; i<counter; j++,i++) {
            charContainer[i]=charArray2[j];
        }
        //converting charContainer char array into string
        return new String(charContainer);
    }
    public static void main(String args[]){
        String str1 = "FirstString";
        String str2 = "SecondString";
        String strContainer = strcat(str1,str2);
        System.out.println("String Container : " +strContainer);  
    }
}//end of the class
Comments