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:
- Define the main method:
- The program starts executing from the main method.
- 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.
- 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.
- 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.
- 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:
- Define the main method:
- The program starts executing from the main method.
- 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.
- 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.
- 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.
- 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).
Leave a Reply