0
votes

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

5

5 Answers

5
votes

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
0
votes
import java.io.*;
class Concatenation
{
    public static void main(String []args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int i=0;
        String s1,s2;
        System.out.print("Enter the first string:");
        s1=br.readLine();
        System.out.print("Enter the Second string:");
        s2=br.readLine();
        char s3[]=new char[s1.length()+s2.length()];
        for(;i<s1.length();i++)
            s3[i]=s1.charAt(i);
        for(int j=0;j<s2.length();j++)
            s3[i++]=s2.charAt(j);
        System.out.println("Result:"+new String(s3));
    }
}
0
votes
public class Conc {
    String s1="java",s2="programming",s3="";
    int l=s1.length(),m=s2.length(),i,j;
    public String conca(String s1,String s2){
        for(i=0;i<=l-1;i++){
            s3+=s1.charAt(i);
        }
        for(j=0;j<=m-1;j++){
            s3+=s2.charAt(j);
        }
        System.out.println(s3);
return s3;
    }
    public static void main(String[] args) {

        Conc obj1=new Conc();
        obj1.conca("java","programming");

}
}
0
votes
public class StringConcatination {

    public static String concate(String s1,String s2){
        String s3="";
        for(int i=0;i<s1.length();i++){
            s3+=s1.charAt(i);
        }
        for(int j=0;j<s2.length();j++){
            s3+=s2.charAt(j);
        }

        return s3;
    }

    public static void main(String[] args) {
        System.out.println(concate("java","programming"));
    }    
}
0
votes
public static void concat(String a, String b) {
    /**
     * String result = a + b;
     */

    /**
     * Logic goes here.
     * 
     * Need to iterate through chars
     * 
     * Need to get total length
     */
    int totalLength = a.length();
    totalLength += b.length();
    char[] result = new char[totalLength];
    char[] arrayFromA = a.toCharArray();
    char[] arrayFromB = b.toCharArray();
    int count = 0;
    System.out.println("Total Length of String: "+ totalLength);
    for (int i = 0; i < arrayFromA.length; i++) {
        result[i] = arrayFromA[i];
        count++;
    }
    for (int j = 0; j < arrayFromB.length; j++) {
        result[count] = arrayFromB[j];
        count++;
    }
    System.out.println(new String(result));
}