Guru gab seinen Schülern eine Aufgabe. Er gab einen Satz vor, und die Schüler müssen das erste und das letzte Wort vertauschen und alle Mittelzeichen vertauschen. Helfen Sie den Schülern, diese Aufgabe mit einem Java-Programm zu lösen
Anforderungen:
-
Code: Select all
The words present in the sentence must be more than 2, else print "Invalid Length"
-
Code: Select all
The word should contain only alphabets and space, else print " is an invalid sentence"
-
Code: Select all
In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.
-
Code: Select all
Ensure to follow the object-oriented specifications provided in the question description.
-
Code: Select all
Ensure to provide the names for classes, attributes, and methods as specified in the question description.
-
Code: Select all
Adhere to the code template, if provided
Beispieleingabe/-ausgabe 1:
Geben Sie den Satz ein
Tragen Sie Ihre Maske
mask ruoy raew uoy Do
Sample Input/Output 2:
Geben Sie den Satz ein
Kartenleser
Ungültige Länge
Beispieleingabe/-ausgabe 3:
Geben Sie den Satz ein
Empfehle @ Freund
Empfehle @ Freund ist ein ungültiger Satz
Code:-
Code: Select all
import java.util.Scanner;
class SentenceProcessor {
// Method to check if the sentence is valid
public boolean isValidSentence(String sentence) {
return sentence.matches("[a-zA-Z ]+"); // Only alphabets and spaces allowed
}
// Method to process the sentence
public String processSentence(String sentence) {
if (!isValidSentence(sentence)) {
return sentence + " is an invalid sentence";
}
String[] words = sentence.trim().split("\\s+"); // Split by whitespace
if (words.length