0

我想将 2 个字符串 str1、str2 的值分别存储到第三个字符串 strContainer 中(不使用库方法)。

我的算法是:
1. 将 str1 和 str2 分别转换为字符数组 charArray1 和 charArray2。
2.计算计数器变量中两个字符数组的长度之和(charArray和charArray2的长度之和)
3.两个char数组的和(作为计数器)相当于一个新的char数组charContainer。
4. 循环如下
charContainer[ith index] += charArray1[ith index];

charContainer[ith index] += charArray2[ith index];
5. 将charContainer转换为字符串,显示为strContainer。


到目前为止我的代码:

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

问题:
如何为字符串 str1 和 str2 调用countingLength() 方法?
如何将 charContainer 的索引值指定为计数器(两个 char 数组的总和)?

4

5 回答 5

5

如何调用 StringLengthCounter() 方法?我看不到任何具有该名称的方法。对不起,但这不是这里的问题,问题是这甚至不是有效的代码。

我并不是要严厉,但到处都有 sintax 错误,而且程序逻辑在很多方面都是错误的。

请查看以下代码并尝试弄清楚它是如何工作的,我认为它可以满足您的需求。如果有不清楚的地方问。

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
于 2013-06-23T22:44:37.430 回答
0
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));
    }
}
于 2015-08-14T05:33:21.600 回答
0
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");

}
}
于 2016-02-19T11:17:13.993 回答
0
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"));
    }    
}
于 2016-06-24T14:52:20.037 回答
0
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));
}
于 2019-03-07T09:52:13.813 回答