Print Each Character No of Times Based on Next Occurrence of Numeric Value in a given String using Java Program Eg 1: Input: a1b10 Output: abbbbbbbbbb
Write a program to give the following output for the given input
Program in JAVA:
Eg 1: Input: a1b10
Output: abbbbbbbbbb
Eg: 2: Input: b3c6d15
Output: bbbccccccddddddddddddddd
The number varies from 1 to 99.
Program in JAVA:
public class PrintString { public static void main(String[] args) { String n="a10b5c03"; char []a; char charValue = 0; int i,j,count=0,numberValue=0; a=n.toCharArray(); for(i=0;i<a.length;i++) { if(Character.isDigit(a[i])) { while(Character.isDigit(a[i])) { numberValue=Character.getNumericValue(a[i]); count=(count*10)+numberValue; i++; if(i>(a.length-1)) { i--; break; } } while(Character.isAlphabetic(a[i])) { i--; break; } for(j=0;j<count;j++) { System.out.print(charValue+" "); } count=0; } else { charValue=a[i]; } } } }
Output:
Comments
Post a Comment