java-不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()) {
			String word1=cin.next();
			String word2=cin.next();
			String newWord=word1+word2;
			System.out.println(newWord);
		}
		cin.close();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用双指针的方法,分别指向两个字符串的末尾,然后从后往前遍历,将两个字符串的对应字符相加,得到新的字符串。重复次数最多的字符串可以使用哈希表来实现,遍历新字符串,将每个子串出现的次数记录在哈希表中,最后找到出现次数最多的子串即可。 ### 回答2: 要实现不使用库函数拼接两个字符串并输出重复次数最多的字符串,可以先创建两个字符串变量,分别用来存储两个字符串。然后使用循环遍历每个字符,将其逐个拼接到对应的字符串变量中,直到两个字符串全部拼接完成。 具体步骤如下: 1. 创建两个字符串变量str1和str2。 2. 使用循环遍历第一个字符串,将每个字符逐个拼接到str1。 3. 使用循环遍历第二个字符串,将每个字符逐个拼接到str2。 4. 创建一个HashMap对象来存储字符串及其出现次数的键值对。 5. 使用循环遍历str1和str2中的每个字符,在HashMap中进行统计。 - 如果HashMap中已经存在该字符,则将其对应的值加1。 - 如果HashMap中不存在该字符,则将该字符添加到HashMap中,并设置其对应的值为1。 6. 创建一个变量maxCount,用来记录出现次数最多的字符串的出现次数。 7. 使用循环遍历HashMap中的每个键值对,找到出现次数最多的字符串,并将其出现次数赋值给maxCount。 8. 再次使用循环遍历HashMap中的每个键值对,找到出现次数等于maxCount的字符串,输出即可。 示例代码如下: ``` import java.util.HashMap; public class Main { public static void main(String[] args) { String str1 = "abc"; String str2 = "def"; // 拼接字符串 for (char c : str2.toCharArray()) { str1 += c; } // 统计字符串出现次数 HashMap<String, Integer> countMap = new HashMap<>(); int maxCount = 0; for (char c : str1.toCharArray()) { String s = String.valueOf(c); int count = countMap.getOrDefault(s, 0) + 1; countMap.put(s, count); if (count > maxCount) { maxCount = count; } } // 输出重复次数最多的字符串 for (String s : countMap.keySet()) { if (countMap.get(s) == maxCount) { System.out.println(s); } } } } ``` 上述代码中,首先拼接了两个字符串str1和str2。然后使用HashMap统计了拼接后的字符串中每个字符出现的次数,并找到了出现次数最多的字符串。最后输出了重复次数最多的字符串。 ### 回答3: 要实现不使用库函数拼接两个字符串,可以使用字符数组或StringBuilder类来完成。 使用字符数组的方法如下: 1. 定义两个字符串str1和str2。 2. 创建一个字符数组result,长度为str1的长度加上str2的长度。 3. 使用for循环将str1中的每个字符依次赋值给result数组的对应位置。 4. 使用for循环将str2中的每个字符依次赋值给result数组的对应位置。 5. 将result转换成字符串,并输出。 使用StringBuilder类的方法如下: 1. 定义两个字符串str1和str2。 2. 创建一个StringBuilder对象result,将str1追加到result中。 3. 使用result的append()方法将str2追加到result中。 4. 将result转换成字符串,并输出。 输出重复次数最多的字符串可以使用HashMap来实现统计。 1. 定义一个HashMap<String, Integer>类型的变量countMap,用于存储字符串和出现次数的映射关系。 2. 使用for循环遍历拼接后的字符串,将每个字符串作为key,出现次数作为value存入countMap。 3. 定义一个变量maxCount,用于记录最大的出现次数,初始值为0。 4. 使用for-each循环遍历countMap的entrySet,找到出现次数最大的字符串,并更新maxCount的值。 5. 输出出现次数最多的字符串。 示例代码如下: ```java import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; // 使用字符数组拼接字符串 char[] result = new char[str1.length() + str2.length()]; for (int i = 0; i < str1.length(); i++) { result[i] = str1.charAt(i); } for (int i = 0; i < str2.length(); i++) { result[str1.length() + i] = str2.charAt(i); } String concatenatedString = new String(result); System.out.println("拼接后的字符串:" + concatenatedString); // 使用StringBuilder拼接字符串 StringBuilder sb = new StringBuilder(str1); sb.append(str2); concatenatedString = sb.toString(); System.out.println("拼接后的字符串:" + concatenatedString); // 统计出现次数最多的字符串 Map<String, Integer> countMap = new HashMap<>(); for (char c : concatenatedString.toCharArray()) { String s = String.valueOf(c); int count = countMap.getOrDefault(s, 0) + 1; countMap.put(s, count); } String maxCountString = null; int maxCount = 0; for (Map.Entry<String, Integer> entry : countMap.entrySet()) { if (entry.getValue() > maxCount) { maxCount = entry.getValue(); maxCountString = entry.getKey(); } } System.out.println("重复次数最多的字符串:" + maxCountString); } } ``` 以上代码输出的结果为: 拼接后的字符串:HelloWorld 重复次数最多的字符串:l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值