Write A program to expand a given array
input-{'A','3','B','2','C','1'}
output-{A,A,A,B,B,C}
public class CountArray {
char[] arr = new char[100];
int index = 0;
public static void main(String[] args) {
CountArray ca = new CountArray();
char[] brr = { 'A', '2', 'B', '1', 'C', '5', 'D', '4' ,'E','6'};
for (int j = 0; j < brr.length; j++) {
boolean flag = ca.getNumber(brr[j]);
if (!flag) {
int n = ca.getNum(brr[j + 1]);
ca.makeList(brr[j], n);
}
}
ca.print();
}
public void print() {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
public int getNum(char c) {
int zeroAscii = (int) '0';
int charAscii = (int) c;
int num = (charAscii - zeroAscii);
return (num);
}
public void makeList(char ch, int n) {
int j = index;
for (int i = j; i < j + n; i++) {
arr[index++] = ch;
}
}
public boolean getNumber(char c) {
int zeroAscii = (int) '0';
int charAscii = (int) c;
int num = (charAscii - zeroAscii);
return (num <= 9 && num >= 0);
}
}
Output:
A A B C C C C C D D D D E E E E E E
No comments:
Post a Comment