Answer to Question #257505 in Java | JSP | JSF for Nikhil Sahoo

Question #257505

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.


1
Expert's answer
2021-10-27T16:23:22-0400
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;
            }
        }
    }
    

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS