Print Each Character 2 Times: This Java program aims to generate an output array based on a given input array. The input array is defined as {‘A’, ‘B’, ‘C’, ‘D’}. The desired output is an array where each element is a concatenation of the corresponding elements from the input array.
Print Each Character 2 Times Of Character Array
For example, given the input array {A, B, C, D}, the expected output is {AA, BB, CC, DD}.
package com.softwaretestingo.interviewprograms; public class InterviewPrograms10 { /* * Input:{A, B, C, D} * Output:{AA, BB, CC, DD} */ public static void main(String[] args) { char[] ch = {'A', 'B', 'C', 'D'}; append(ch); } static void append(char ch[]) { char[] ch1=ch.clone(); System.out.println(ch); System.out.println(ch1); char[] result= new char[0]; StringBuilder sb = new StringBuilder(); for(int i=0;i<ch.length;i++) { sb.append(ch[i]); sb.append(ch1[i]); sb.append(' '); result = sb.toString().toCharArray(); } System.out.println(result); } }
Output:
AA BB CC DD
Explanation:
Initialize the input array: We create an array ch containing the characters ‘A’, ‘B’, ‘C’, and ‘D’. This serves as the input for our program.
char[] ch = {'A', 'B', 'C', 'D'};
Call the append method: We pass the input array ch as an argument to the append method for further processing.
append(ch);
Define the append method: This method takes the input array ch
and performs the necessary operations to generate the output array.
static void append(char ch[]) { // method code goes here }
Clone the input array: We create a clone of the input array ch and assign it to the array ch1. This is done to preserve the original input array for future use.
char[] ch1 = ch.clone();
Generate the output array: Using a StringBuilder, we iterate over the input array ch and append the corresponding element from the cloned array ch1 to create a pair. A space separates each pair. The resulting string is then converted to a character array and assigned to the result array. Finally, we print the generated output array result.
char[] result = new char[0]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < ch.length; i++) { sb.append(ch[i]); sb.append(ch1[i]); sb.append(' '); result = sb.toString().toCharArray(); } System.out.println(result);
In summary, this program takes an input array of characters, clones it, and generates an output array by combining each element of the input array with its corresponding element in the cloned array. The resulting pairs are stored in a string, which is then converted into an output array for display.
Alternative Way:
package com.softwaretestingo.interviewprograms; import java.util.ArrayList; public class InterviewPrograms10_1 { /* * Input:{A, B, C, D} * Output:{AA, BB, CC, DD} */ public static void main(String[] args) { String[]arr= {"A","B","C","D"}; ArrayList<String> AL=new ArrayList<>(); for(int i=0;i<arr.length;i++) { String str=arr[i]+arr[i]; AL.add(str); } //System.out.println(arr); System.out.println(AL); } }
Output:
[AA, BB, CC, DD]
Explanation:
- Initialize the input array: We create an array arr containing the strings “A”, “B”, “C”, and “D”. This serves as the input for our program.
- Create an ArrayList: We create an ArrayList named AL to store the generated output.
- Generate the output list: We iterate over the input array arr using a loop. In each iteration, we concatenate the current element of arr with itself to create a new string str. The concatenated string is then added to the ArrayList AL.
for (int i = 0; i < arr.length; i++) { String str = arr[i] + arr[i]; AL.add(str); }
In summary, this program takes an input array of strings, generates an output list by concatenating each element of the input array with itself, and stores the results in an ArrayList. The final output is displayed by printing the ArrayList. The program demonstrates the usage of loops, string concatenation, and ArrayList in Java.
Leave a Reply