Write a program to take a String name as input then display the name's first and last words as it was and the middle will be only first letter using substring and indexOf.
Example:
Enter a String
Meser chen Rixou xang
Meser c.R. xang
Don't use array and split
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String line = in.nextLine();
int start = 0;
while (true) {
int index = line.substring(start).indexOf(' ');
if (index == -1) {
System.out.print(line.substring(start));
break;
}
if (start != 0) {
System.out.print(line.substring(start, start + 1) + ". ");
} else {
System.out.print(line.substring(start, start + index + 1));
}
start += index + 1;
}
}
}
Comments
Leave a comment