Write a program to take a String name as input without using array and display the name in the format that even words will be first letter only.
Enter a String
Hun leij cenr xanf ewasn
Hun l. cenr x. eswasn
Hint: Use trim(), indexOf(), lastIndexOf(), charAt(), substring() if necessary.
Don't use Character class and 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 inputString = in.nextLine();
boolean isOneLtter = false;
int c = 0;
for (int i = 0; i < inputString.length(); i++) {
if (isOneLtter) {
if (c == 0) {
System.out.print(inputString.charAt(i) + ". ");
}
c++;
} else {
System.out.print(inputString.charAt(i));
}
if (inputString.charAt(i) == ' ') {
isOneLtter = !isOneLtter;
c = 0;
}
}
in.close();
}
}
Comments
Leave a comment