Source

Source: variation on CodeChef problem NITIKA: “Whats in the Name”

Description

Given a string fullname representing a person’s full name:

  • if fullname contains only one name, capitalize it
  • if fullname contains more than one name, replace all but the final name with an initial followed by a period.
1
2
3
eliot ⟹ Eliot
thomas eliot ⟹ T. Eliot
thomas stearns eliot ⟹ T. S. Eliot

My solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class initialize {
    
    public static String initialize(String fullname) {
        String[] names = fullname.split(" ");
        int nameslen = names.length;
        String last = names[nameslen - 1];
        names[nameslen - 1] = last.substring(0, 1).toUpperCase() +
                              last.substring(1);
        for (int i = 0; i < nameslen - 1; i++) {
            String curr = names[i];
            names[i] = curr.substring(0, 1).toUpperCase() + ".";
        }
        return String.join(" ", names);
    }
    
}

Tests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.junit.*;
import static org.junit.Assert.*;

public class initializeTest {
    initialize x = new initialize();

    @Test
    public void testSingleName() {
        assertEquals(x.initialize("eliot"), "Eliot");
    }
    
    @Test
    public void testOneInitial() {
        assertEquals(x.initialize("thomas eliot"), "T. Eliot");
    }
    
    @Test
    public void testTwoinitials() {
        assertEquals(x.initialize("thomas stearns eliot"), "T. S. Eliot");
    }
}