Regexp fun

Reading the post about useful regular expressions,  remembered what my favourite solution is to one of  the questions of the test we give to junior Java developers.

The task is to write a method that takes a string as a parameter and returns the acronym of the string in uppercase made up of the first letters of the words in the string. The acronym must ignore the words “the”, “of” and “and”.

The usual solutions are either to sequentially step through the string (Yuck!) or split it up or use a StringTokenizer class. The people usually overlook the fact, that the input strings can be padded with whitespace, or contain multiple spaces, and they usually ignore, that the keywords that are to be omitted might be found on the begining of a valid word. Thus my test ” United   States of Andorra” string breaks most of the methods.  The ones who have time to write the answer down, usually forget to return the value from the method, or to change it to uppercase and sometimes even ignore that it should be a method to start with! This is my favourite question, as it can really show how the applicant can handle stressful situations.

I was tired after several interviews one day and tried to come up with the  shortest possible solution. Naturally it contains regular expressions.

My solution looked something like this (OK I just reproduced it for the sake of the article, using nano and javac, so it might have overlooked flaws in it):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Acronym { 
 
private static String toAcronym(String str) {
        return str.toUpperCase().
                        replaceAll("(THE|OF|AND)(\W+|$)","").
                        replaceAll("(\w)\w*\W*","$1");
}
 
public static void main (String args[]) {
                if(args.length>0) {
                        System.out.println(toAcronym(args[0]));
        }
}
 
}

Leave a Reply

Your email address will not be published. Required fields are marked *