• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP To Print Each Character 2 Times Of Character Array

WAP To Print Each Character 2 Times Of Character Array

Last Updated on: July 27, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Print Each Character 2 Times Of Character Array

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.

    Java Array Programs
    Java Array Programs
    Duplicate Characters In a String Java
    Find Duplicate Characters In a String Java
    WAP to Verify Balanced Parentheses In Java
    WAP to Verify Balanced Parentheses In Java
    Order Character Based On Number Of Occurrences
    WAP to Order Character Based On Number Of Occurrences
    WAP to Reverse Words Without Changing Digits Position
    WAP to Reverse Words Without Changing Order & Digits Position
    WAP For Removing White Spaces In A String
    WAP For Removing White Spaces In A String
    WAP to Separate Two Characters With Digit
    WAP to Separate Two Characters With a Digit
    WAP To Reverse String Without Using Inbuilt Functions
    WAP To Reverse String Without Using Inbuilt Functions
    Decimal to Binary How To Conversion in Java
    Convert Decimal to Binary
    Fibonacci Series Numbers Sequence Program
    Fibonacci Series Numbers Sequence Program

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers