Problem:
Write a program that inputs a string from the user. The string will contain alphabets, numbers, special characters and question marks (which is a special character itself). We have to determine if there are exactly three question marks between each pair of numbers that add up to 10. If so, print true on the screen otherwise print false on the screen. Some example test cases are given below:
Code:1 2 3 4 5 | : "arrb6???4xxb15???eee5" //true : "acc?7??ss?3rr1??????5" //true : "5??aaaaaaaaaaaaaa?5?5" //false : "9???1???9???1???9" //true : "aa6?9" //false |
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /** * @author Hafiz Muhammad Umer */ import java.util.Scanner; public class QuestionMark { public static void main(String[] args) { Scanner input=new Scanner(System.in); String in = input.nextLine(); char[] inc; inc=in.toCharArray(); int tester,dec=1,sum,de2=0; for(int n1=0;n1<inc.length;n1 ) { tester=inc[n1]; if (tester>47 && tester<58) { for (int n2=(n1 1);n2<inc.length;n2 ) { tester=inc[n2]; if (tester>47 && tester<58) { if ((Integer.parseInt(in.substring(n1, n1 1)) Integer.parseInt(in.substring(n2, n2 1)))==10) { de2=1; sum=0; for(int n3=(n1 1);n3<n2;n3 ) { if (inc[n3] == '?') { sum =1; } } if (sum!=3) { dec*=0; } } break; } } } } dec*=de2; if(dec==1) System.out.println("True"); else System.out.println("False"); } } |
afs5f??dg?53???7llpv True
fg5?ljg?ggdfugyf?5???3?f??fhgh?fht7???3 False
Explanation:
- The program first inputs a string from the user at line 10 and converts it into a char array at line 12.
- At line 14, a loop starts and an if statement at line 17 tests each element of the char array to determine if it is a number. If this is a number, then the another loop starts at line 19 inside which another if statement at line 22 checks the next number in the string. When the position of two numbers is determined in the string, an if statement at line 24 determines whether these two numbers add to 10 or not. If they add to 10, another loop at line 28 will start inside which an if statement will be used to count the number of question marks between the two numbers. If three question marks are not present, the value of the variable dec will turn 0 due to an if statement at line 36. The loop ends and the same procedure starts again.
- At last, the value of dec variable is examined at line 47. If it is the default i.e 1 assigned in the line 13, then the program displays true and if it is changed due to the if statement at line 36, the program will display false on the screen. Please note that the if statement at line 36 will change the value of the dec variable if exactly three question marks do not exist between each pair of numbers that add to 10.
- Please take a look at the example 4 in the problem. The 9 at the first place and the 1 at the 13th place add to 10 but there are 9 question marks between them. Still the output should be true. It means we have to compare a number in the string with only one number which is next to it in the string and not the other ones. Due to this the 9 at first place should only be compared with 1 at 5th place and not with the 1 at 13th place. For this purpose, I have placed a break statement at line 41 which will terminate the loop after a number is compared with another number next to it in the string.
- Now take a look at the example 5 in the problem. In this case no pair of numbers add to 10 and the result is false. It means that we will have to check the string in the program and if no pair of numbers add to 10, the program should return false. For this purpose, I have defined a variable de2 at line 13 with initial value of zero. Its value is altered only at line 26 in the program. Please see the structure of the program. The control will only approach the line 26 if it passes the condition of the if statement at line 24 and the condition is that the two numbers add to 10. It means that if any two pair of numbers in the string add to 10, the value of de2 will be 1 due to line 26 and if no pair of numbers add to 10 in the string, its value will remain 0. Before displaying the result, the de2 is multiplied with the dec variable due to which the final result will now also depend upon the de2 variable. In short, the program will not be able to return true if no pair of numbers in the string add to 10.
0 Comments
Thanks for leaving a comment. I will try to reply you soon.