• 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 Separate Two Characters With a Digit

WAP to Separate Two Characters With a Digit

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

What We Are Learn On This Post

  • Separate Two Characters With a Digit

Separate Two Characters With a Digit: The given Java program takes an input string AB2C99423A and rearranges its characters to separate alphabetic and non-alphabetic characters. The resulting output is A2B9C9A423.

Separate Two Characters With a Digit

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms41 
{
	/*
	 * String input= AB2C99423A 
	 * String output =A2B9C9A423
	 */
	public static void main(String[] args) 
	{
		String s = "AB2C99423A";
		char[] cArr = s.toCharArray();
		char resultArr[] = new char [cArr.length*2];
		int evenCount = 0 ;
		int oddcount = 1 ;
		for ( int i=0 ; i<s.length(); i++ )
		{
			if ( Character.isAlphabetic(cArr[i]))
			{
				resultArr [evenCount] = cArr[i];
				evenCount = evenCount+2;
			} 
			else if(!Character.isAlphabetic(cArr[i]))
			{
				resultArr [oddcount]= cArr[i];
				oddcount = oddcount+2 ;
			}
		}
		System.out.println(resultArr);
	}
}

Output

A2B9C9A4

Here’s a step-by-step explanation of the program:

  1. Define the main method:
    • The program starts executing from the main method.
  2. Initialize the input string and convert it to a character array:
    • The input string s is initialized with the value “AB2C99423A”.
    • The toCharArray() method is used to convert the input string into a character array cArr.
  3. Initialize the result array:
    • A new character array resultArr is created with twice the length of the original character array cArr. This is because the result array will contain both alphabetic and non-alphabetic characters, and we need space for both.
  4. Separate alphabetic and non-alphabetic characters:
    • Two variables, evenCount and oddcount, are initialized to keep track of the indices for alphabetic and non-alphabetic characters in the resultArr, respectively.
    • The program iterates through each character in the input string using a loop.
    • If the character is alphabetic (determined using Character.isAlphabetic(cArr[i])), it is placed at the evenCount index in the resultArr, and then evenCount is incremented by 2 to maintain space between alphabetic characters.
    • If the character is non-alphabetic (not alphabetic), it is placed at the oddcount index in the resultArr, and then oddcount is incremented by 2 to maintain space between non-alphabetic characters.
  5. Print the output:
    • The program prints the resultArr, which contains the rearranged characters with alphabetic and non-alphabetic characters separated.

For the given input “AB2C99423A”, the program rearranges the characters as “A2B9C9A423”. Alphabetic characters (A, B, C, A) are separated by non-alphabetic characters (2, 9, 9, 3).

Alternative Way 1:

The given Java program takes an input string AB2C99423A and rearranges its characters to separate alphabetic and numeric characters. The resulting output is A2B9C9A423.

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms41_1 
{
	/*
	 * String input= AB2C99423A 
	 * String output =A2B9C9A423
	 */
	public static void main(String[] args) 
	{
		String inpu = "AB2C99423A";
		String charac = "";
		String intege = "";
		String result="";
		for(int i=0;i<inpu.length();i++) 
		{
			if(Character.isLetter(inpu.charAt(i))) 
			{
				charac = charac + inpu.charAt(i);
			}
			else if(Character.isDigit(inpu.charAt(i))) 
			{
				intege = intege + inpu.charAt(i);
			}
		}
		int index1=0, index2=0;
		for(int i=0;i<inpu.length();i++) 
		{
			if(i%2==0 && charac.length()>index1) 
			{
				result = result + charac.charAt(index1);
				index1++;
			}
			else 
			{
				if(intege.length()>index2)
				{
					result = result + intege.charAt(index2);
					index2++;
				}
			}
		}
		System.out.println(result);
	}
}

Output

A2B9C9A423

Here’s a step-by-step explanation of the program:

  1. Define the main method:
    • The program starts executing from the main method.
  2. Initialize the input string and other required variables:
    • The input string inpu is initialized with the value “AB2C99423A”.
    • Three empty strings, charac, intege, and result, are initialized to store alphabetic characters, numeric characters, and the final rearranged string, respectively.
  3. Separate alphabetic and numeric characters:
    • The program iterates through each character in the input string using a loop.
    • If the character is a letter (alphabetic character), it is appended to the charac string.
    • If the character is a digit (numeric character), it is appended to the intege string.
  4. Rearrange the characters:
    • The program uses two indices, index1 and index2, to keep track of the current positions in the charac and intege strings, respectively.
    • The program iterates through each character in the input string using another loop.
    • If the current index is even, the character at the corresponding position in the charac string is added to the result string, and index1 is incremented.
    • If the current index is odd, the character at the corresponding position in the intege string is added to the result string, and index2 is incremented.
  5. Print the output:
    • The program prints the result string, which contains the rearranged characters with alphabetic and numeric characters separated.

For the given input “AB2C99423A”, the program rearranges the characters as “A2B9C9A423”. Alphabetic characters (A, B, C, A) are separated by numeric characters (2, 9, 9, 3).

    WAP to Format a Phone Number in Human-Readable View
    WAP to Format a Phone Number in Human-Readable View
    Substring Matching In Java
    Substring Matching In Java
    Java Program Interview Concatenate Characters From Each String
    WAP to Concatenate Characters From Each String
    Multiplication Table In Java
    Multiplication Table In Java
    WAP to Find all the Permutations of a String
    WAP to Find all the Permutations of a String
    WAP For Sorting Characters Alphabetically Ignoring Case Sensitive
    WAP For Sorting Characters Alphabetically Ignoring Case Sensitive
    Order Character Based On Number Of Occurrences
    WAP to Order Character Based On Number Of Occurrences
    WAP To Reverse String Without Using Inbuilt Functions
    WAP To Reverse String Without Using Inbuilt Functions
    Reverse Words without Changing Order & Split
    WAP to Reverse Words without Changing Order & Split
    Find the Missing and Duplicate Numbers in the Array
    Find the Missing and Duplicate Numbers in the Array

    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