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) ?