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();
while (true) {
int space = line.indexOf(' ');
String word = line.substring(0, space == -1 ? line.length() : space);
System.out.println(word + " " + word.length() + " " + (word.length() > 1 ? word.substring(word.length() - 1)
+ word.substring(1, word.length() - 1) + word.substring(0, 1) : word));
line = line.substring(space + 1);
if (space == -1) {
break;
}
}
}
}
Do the coding so that output will remain same but ternary operator will not be used.
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String line = in.nextLine();
while (true) {
int space = line.indexOf(' ');
String word = line.substring(0, line.length());
if (space != -1) {
word = line.substring(0, space);
}
if (word.length() > 1) {
System.out.println(word + " " + word.length() + " " + (word.substring(word.length() - 1)
+ word.substring(1, word.length() - 1) + word.substring(0, 1)));
} else {
System.out.println(word + " " + word.length() + " " + word);
}
line = line.substring(space + 1);
if (space == -1) {
break;
}
}
in.close();
}
}
Comments
Leave a comment