Tag Archives: Strings

Abbreviate a name

Follow me

Vinay Singh

Computer Teacher at Saint John's Academy
I am a GNU/Linux enthusiast and a computer science teacher by profession based at Allahabad, India. My hobbies include reading (mostly computer related stuff) and photography.
Follow me

Latest posts by Vinay Singh (see all)

Abbreviate a name means to take out the first letter of each word of the name and combine it with last name in its entirety. For example, Bhagat Singh will abbreviate to B. Singh and Mohanddas Karamchand Gandhi would abbreviate to M.K. Gandhi. It’s being assumed that the string contains only one space between each word and there are no leading or trailing spaces. The problem to abbreviate a name can be approached in various ways and in this post we will see some major approaches. The methods to abbreviate a name which I will be giving in the beginning will emphasize on ease of use and the ones discussed towards the end will emphasize on efficiency. Students of classes 9 and 10 (ICSE) can use the easier or the efficient methods but students of classes 11 and 12 (ISC) should focus on the efficient versions only.

Abbreviate a name using the library function split()

String class has a function split() which splits the string around matches of the given string or a regular expression. Students need not worry regarding regular expression at this stage as they are not in the ICSE or ISC syllabus. If a string str contains “Bhagat Singh”, then str.split(” “), would split the string around a space and return a string array containing each word of the string. In this case the returned String array would be {“Bhagat”, “Singh”}. In case of “Mohanddas Karamchand Gandhi”, the returned String array would be {“Mohandas”, “Karamchand”, “Gandhi”}. The program to abbreviate a name using split() function is as follows:

class Abbreviate{
    public static String abbreviateName(String str){
        String ans="";
        String arr[] = str.split(" ");
        for(int i=0; i < arr.length-1 ; i++){
            ans += arr[i].charAt(0) + ". ";
        }
        return ans+arr[arr.length-1];
    }
    public static void main(String args[]){
        System.out.println( abbreviateName( "Bhagat Singh" ) );
        System.out.println( abbreviateName( "Mohandas Karamchand Gandhi" ) );
        System.out.println( abbreviateName( "Singh" ) );
    }
}

Abbreviate a name using the library function StringTokenizer()

The StringTokenizer class declared in the package java.util, allows an application to break a string into tokens. The program to abbreviate a name using split() function is as follows:

import java.util.*;
class Abbreviate{
    public static String abbreviateName(String str){
        String ans="";
        StringTokenizer st = new StringTokenizer(str);
        int numberOfWords = st.countTokens();
        for(int i=0 ; i <  numberOfWords-1 ; i++){
            ans += ( st.nextToken() ).charAt( 0 ) + ". ";
        }
        return ans+st.nextToken();
    }

    public static void main(String args[]){
        System.out.println( abbreviateName( "Bhagat Singh" ) );
        System.out.println( abbreviateName( "Mohandas Karamchand Gandhi" ) );
        System.out.println( abbreviateName( "Singh" ) );
    }
}

Abbreviate a name using only the String class functions

The program to abbreviate a name using only the String class functions is as follows:

import java.util.*;
class Abbreviate{
    public static String abbreviateName(String str){
        str=" " + str;
        String ans="";
        int length = str.length(), lastSpace=-1;
        char ch;
        for(int i=length-1 ; i >= 0 ; i--){
            ch=str.charAt(i);
            if(ch==' '){
                lastSpace=i;
                break;
            }
        }
        if(lastSpace==-1) return str;
        for(int i = 0 ; i < lastSpace ; i++){
            ch=str.charAt(i);
            if(ch==' '){
                ans += str.charAt( i+1 ) + ". ";
            }
        }
        ans += str.substring(lastSpace+1);
        return ans;
    }

    public static void main(String args[]){
        System.out.println( abbreviateName( "Bhagat Singh" ) );
        System.out.println( abbreviateName( "Mohandas Karamchand Gandhi" ) );
        System.out.println( abbreviateName( "Singh" ) );
    }
}

In the above program please note that though there are two for loops, the total number of iterations would be equal to the number of characters in the string. The program adds a single space in the beginning of the string and then find the last occurrence of the space character in the string by taking a reverse loop from the length-1 to 0th position. The loop aborts as soon as a space is encountered. If no space is left the variable lastSpace retains its initial value that is -1. If after the end of first for loop the value of lastSpace is -1 (that is no space is found) then the string is returned as it is else the surname is extracted using the substring function. The surname will be the entire string starting from lastSpace+1 to the end.

The program to abbreviate a string given above can be done without the substring() function by introducing a separate string variable for surname. This surname can be formed by string concatenation at the time of searching for the last space. The program for the same as follows:

import java.util.*;
class Abbreviate{
    public static String abbreviateName(String str){
        str=" " + str;
        String ans="", surname="";
        int length = str.length(), lastSpace=-1;
        char ch;
        for(int i=length-1 ; i >= 0 ; i--){
            ch=str.charAt(i);
            if(ch==' '){
                lastSpace=i;
                break;
            }else surname = ch+surname;
        }
        if(lastSpace==-1) return str;
        for(int i = 0 ; i < lastSpace ; i++){
            ch=str.charAt(i);
            if(ch==' '){
                ans += str.charAt( i+1 ) + ". ";
            }
        }
        ans += surname;
        return ans;
    }

    public static void main(String args[]){
        System.out.println( abbreviateName( "Bhagat Singh" ) );
        System.out.println( abbreviateName( "Mohandas Karamchand Gandhi" ) );
        System.out.println( abbreviateName( "Singh" ) );
    }
}

As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.