Write a program that accepts a string as input and converts the first two names into dot-separated initials and prints the output.
The input string format is ‘fn mn ln’. The output string format is ‘ln [mn’s 1st character].[fn’s 1st character]’
Input and Output Format
- Input consists of a string.
- The output consists of a String.
Refer sample output for formatting specifications
Sample Input:
Sachin Ramesh Tendulkar
Sample Output:
Tendulkar R.S
Program for Name Shrinking in Java
Following are steps to shrink name in Java:
- Input string from the user.
- Pass the string to getvalues() method.
- Inside the method, create an empty string buffer to hold the updated string.
- Use a string tokenizer to break strings into tokens i.e first_name, middle_name, and last_name.
- Now, append that last token with a space.
- Extract the substring from the middle name and append it to a string buffer.
- Do the same for the first name and append it to a string buffer.
- At last, print the value in a string buffer.
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
getvalues(s1);
}
public static void getvalues(String s1) {
StringBuffer sb = new StringBuffer();
StringTokenizer st = new StringTokenizer(s1, " ");
String s2 = st.nextToken();
String s3 = st.nextToken();
String s4 = st.nextToken();
sb.append(s4).append(" ");
sb.append(s3.substring(0, 1));
sb.append(".");
sb.append(s2.substring(0, 1));
System.out.println(sb);
}
}
Output
Merge Two String in Java
Write a code to merge two strings in Java. For example, there are two strings “Hi” and “to”. Now, the program should return “Htio”.
Input and Output Format
- Input consists of a string.
- The output consists of a String.
Refer sample output for formatting specifications
Sample Input:
english
marathi
Sample Output:
emnagrlaitshhi
Program to merge two strings in Java
Following are the steps to merge alternative strings:
- Input strings from the user.
- Pass both the string to getvalues() method.
- Inside the method, create an empty string buffer to hold the updated string.
- Now, use the for loop to iterate over the string. Then, fetch the ith character of the first string if it exists and append it to the string buffer.
- Then, take the ith character of the second string and append it to the string buffer.
- At last, print the value in a string buffer.
package com.demo2;
import java.util.Scanner;
public class MergeTwoString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s1 = scanner.nextLine();
String s2 = scanner.nextLine();
mergeTwoString(s1,s2);
}
private static void mergeTwoString(String s1, String s2) {
StringBuffer result = new StringBuffer();
for(int i =0 ;i <s1.length() || i < s2.length();i++){
if(i<s1.length())
result.append(s1.charAt(i));
if(i<s2.length())
result.append(s2.charAt(i));
}
System.out.println(result);
}
}
Output
Thus, in this way, we learn how to shrink a name in Java. Along with this, merge alternative strings.