Write a program in Java to display only those words of a string which carries same letter in both the end and also display how many such words found.
import java.util.Scanner;
public class WordIdentifier {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String line;
//enter the strings
System.out.println("Please enter the strings:");
line=sc.nextLine();
System.out.println("New line of the program:"+line);
//split the entered string which contains the space between the words- (" ")
String[] words = line.split(" ");
// traverse in the string after splitting into the word
for(String word : words) {
// find the word which starts and ends with the same letter
if((word.charAt(0)==word.charAt(word.length()-1))||
(Character.toLowerCase(word.charAt(0))==
Character.toLowerCase(word.charAt(word.length()-1)))
||(Character.toUpperCase(word.charAt(0))==
Character.toLowerCase(word.charAt(word.length()-1)))||
(Character.toLowerCase(word.charAt(0))==
Character.toUpperCase(word.charAt(word.length()-1)))
){
System.out.println("The words which starts and ends with the same letter are:"+word);
}else{
System.out.println("No such words found in the string.");
break;
}
}
}
}
Comments
Leave a comment