Java program to enter a sentence and print the words starting and ending with same alphabets
Answers
Answer:
The split() method of the String class Split the given string into an array of Strings using the split() method of the String class.
In the for loop traverse through each word of the obtained array.
Get the first character of each word in the obtained array using the charAt() method.
Verify if the character is equal to any of the vowels using the if loop if so print the word.
Answer:
import java.util.Scanner;
public class KboatSpecialPalindrome
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = in.next();
str = str.toUpperCase();
int len = str.length();
if (str.charAt(0) == str.charAt(len - 1)) {
boolean isPalin = true;
for (int i = 1; i < len / 2; i++) {
if (str.charAt(i) != str.charAt(len - 1 - i)) {
isPalin = false;
break;
}
}
if (isPalin) {
System.out.println("Palindrome");
}
else {
System.out.println("Special");
}
}
else {
System.out.println("Neither Special nor Palindrome");
}
}
}