Quick actions

cmd+k|ctrl+k

Navigation

Languages

Word Finder

Snippet info

Language

Java

Visibility

public

Author

shraddharao-

Created

2024-02-27T07:28:13.596487Z

Updated

2024-02-27T07:28:13.596487Z

class Main {
    
    /*
Word finding: Given a word w and some text t, write a function that finds whether w is in t.
For example: w="nest", t="I built a nest and tested it."
 */
    
    // write a function named isWordFound that will take two param word, text.
    // Iterate thriugh the text to check if the word found
    // if its found then return true otherwise return false
    // o(m * n)
    
    public static boolean isWordFound(String word, String text){
        // covert both words and text to lowercase for case-sensitive comparision
        word = word.toLowerCase(); //o(1)
        text = text.toLowerCase(); //o(1)
        
        // Split the text into words using whitespace as delimeter
        String[] wordsInText = text.split("\\s+"); 
        
        // iterate through each word in the text
        for(String w : wordsInText){ 
            if(w.equals(word)){   
                return true;
            }
        }
        return false;
        
    }
    public static void main(String[] args) {
        String word = "nest";
        String text = "I built a nest and tested it.";
        boolean isFound = isWordFound(word, text);
        System.out.println(isFound);
    }
}
INFO