Instruction: 73. Write a Java program to create a new string taking first and last characters from two given strings. If the length of either string is 0 use "#" for missing character.
Test Data: str1 = "Python"
str2 = " "
Sample Output:
P#
Link of PB : https://www.w3resource.com/java-exer...asic/index.php
Here is my program for this pb :
try (Scanner input = new Scanner(System.in)) { // get user String input System.out.print("Input Str1 : "); String Str1 = input.next(); System.out.print("Input Str2 : "); String Str2 = input.next(); // check String Str1 & Str2 length for diff case if (Str1.length() != 0 && Str2.length() != 0) { // case a System.out.println(Str1.substring(0, 1) + Str2.substring(Str2.length() - 1, Str2.length())); } else if (Str1.length() != 0 && Str2.length() == 0) { // case b System.out.println(Str1.charAt(0) + "#"); } else System.out.println("#" + Str2.substring(Str2.length() - 1, Str2.length())); // case c }
The issue is that when I test Case b & c it does return me for example P" (case b) or "P (case c). Why it is not P# or #P as I am expecting with for input Python and ""
By the way, Case a works with no issue.