Write a program to take a String name as input then display the signature using substring().
Example:
Enter a String
Chandra Bahadur Dangi Ailen Coq
C. Bahadur D. Ailen C.
Don't use Array & Split
import java.util.*;
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String: ");
String line = in.nextLine();
boolean space = true;
boolean first = false;
for (int i = 0; i < line.length(); i++) {
if (line.substring(i, i + 1).equals(" ")) {
System.out.print(line.substring(i, i + 1));
space = !space;
first = false;
} else if (space) {
if (!first) {
System.out.print(line.substring(i, i + 1) + ".");
first = true;
}
} else {
System.out.print(line.substring(i, i + 1));
}
}
System.out.println();
in.close();
}
}
Comments
Leave a comment