AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-string-handling: Write a program that reads in a String and replace the numerals

Write a program that reads in a String. If a word in the String contains a numeral n (0 < n < 10) then your program must replace that numeral by the character that follows the numeral repeated n times. It can be assumed that there is no numeral at the end of any word.

Sample Input:

I wo3rk a2t Ig5ni6te.

Sample Output:

I worrrk att Ignnnnnitttttte.

corejava x 353
string-handling x 34
Posted On : 2017-07-07 16:55:00.0
profile Divesh - anyforum.in Divesh
101180
up-rate
4
down-rate

Answers


import java.util.Scanner;


public class StringFormatter {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println(replaceNumerals(input.nextLine()));
}

public static String replaceNumerals(String input){
StringBuilder output=new StringBuilder();
for(int i=0;i<input.length();i++){
if(Character.isDigit(input.charAt(i))){
for(int j=1;j<Integer.valueOf(Character.toString(input.charAt(i)));j++){
output.append(input.charAt(i+1));
}
}else{
output.append(input.charAt(i));
}
}
return output.toString();
}
}

Posted On : 2017-07-08 20:21:15
Satisfied : 3 Yes  3 No
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939092
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in