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.
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
String s1;
String s2;
Scanner in=new Scanner(System.in);
System.out.println("Enter first string: ");
s1=in.next();
System.out.println("Enter second string: ");
s2=in.next();
int n = s2.length();
String res = "";
res += (s1.length() >= 1) ? s1.charAt(0) : '#';
res += (n >= 1) ? s2.charAt(n-1) : '#';
System.out.println(res);
}
}
Comments
Leave a comment