India’s Best Job Seekers and Training Platform Forums Java Problems set There is a list of 20 words. 10 of them are…

Tagged: ,

  • There is a list of 20 words. 10 of them are…

     Akash  updated 11 months, 3 weeks ago 2 Members · 2 Posts
  • Akash 

    Administrator
    at

    There is a list of 20 words. 10 of them are good works, and 10 of them are bad words. Write a regex of not more than 25 characters which would tell if given word is good or bad. Input would only contain one of these 20 words.

    Good words: papa, book, home, cars, jolly, sugar, friend, mother, father, bloomiest

    Bad words: ache, slow, torn, slum, boom, rival, wrong, cholera, revenge, arrogant

    Input: book

    Output: Good

    Input: boom

    Output: bad

    Write Java Code using regex pattern

  • Akash 

    Administrator
    at

    import java.util.regex.*;

    public class Main

    {

    public static void main(String[] args)

    {

    String[] arr = {“ache”, “slow”, “torn”, “slum”, “boom”, “rival”, “wrong”, “cholera”, “revenge”, “arrogant”, “papa”, “book”, “home”, “cars”, “jolly”, “sugar”, “friend”, “mother”, “father”, “bloomiest”};

    for(int i =0; i < arr.length; i++) {

    System.out.println(arr[i] + “: ” + test(arr[i]));

    }

    }

    public static String test(String str) {

    Pattern p = Pattern.compile(“^[atrw].*|^sl|boom|^ch”);

    Matcher m = p.matcher(str);

    return m.find() ? “Bad” : “Good”;

    }

    }

Log in to reply.

Original Post
0 of 0 posts June 2018
Now
KodNest Training New Batch is starting on 02nd November 2020. Attend one week free demo classes.Register Now

New Report

Close