-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ6_StringDigitReplace.java
More file actions
27 lines (21 loc) · 963 Bytes
/
Q6_StringDigitReplace.java
File metadata and controls
27 lines (21 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Q6 Given a string of digits, you should replace any digit below 5 with '0' and any digit 5 and above with '1'. Return the resulting string. Note: input will never be an empty string.
import java.util.Scanner;
public class Q6_StringDigitReplace {
public static void main(String[] args) {
System.out.printf("\nQ6 Given a string of digits, you should replace any digit below 5 with '0' and any digit 5 and above with '1'. Return the resulting string. Note: input will never be an empty string\n\n");
Scanner sc = new Scanner(System.in);
System.out.print("Write the digits : ");
String s = sc.nextLine();
String result = "";
for(int i=0;i<s.length();i++){
int a = s.charAt(i);
if(a<53){
result=result + "0";
}
if(a>=53){
result=result + "1";
}
}
System.out.print("\nString is : " + result);
}
}