Program to print the initials of a name with the surname

Given a full name in the form of a string, the task is to print the initials of a name, in short, and surname in full.

Examples:

Input: Devashish Kumar Gupta
Output: D. K. Gupta

Input: Ishita Bhuiya
Output: I. Bhuiya

Approach: The basic approach is to extract words one by one and then print the first letter of the word, followed by a dot(.). For the surname, extract and print the whole word.



Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print the initials
// of a name with the surname
import java.util.*;
  
class Initials {
    public static void printInitials(String str)
    {
        int len = str.length();
  
        // to remove any leading or trailing spaces
        str = str.trim();
  
        // to store extracted words
        String t = "";
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
  
            if (ch != ' ') {
  
                // forming the word
                t = t + ch;
            }
  
            // when space is encountered
            // it means the name is completed
            // and thereby extracted
            else {
                // printing the first letter
                // of the name in capital letters
                System.out.print(Character.toUpperCase(t.charAt(0))
                                 + ". ");
                t = "";
            }
        }
  
        String temp = "";
  
        // for the surname, we have to print the entire
        // surname and not just the initial
        // string "t" has the surname now
        for (int j = 0; j < t.length(); j++) {
  
            // first letter of surname in capital letter
            if (j == 0)
                temp = temp + Character.toUpperCase(t.charAt(0));
  
            // rest of the letters in small
            else
                temp = temp + Character.toLowerCase(t.charAt(j));
        }
  
        // printing surname
        System.out.println(temp);
    }
  
    public static void main(String[] args)
    {
        String str = "ishita bhuiya";
        printInitials(str);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print the initials 
// of a name with the surname 
using System;
  
class Initials { 
      
    public static void printInitials(string str) 
    
        int len = str.Length ;
  
        // to remove any leading or trailing spaces 
        str = str.Trim(); 
  
        // to store extracted words 
        String t = ""
        for (int i = 0; i < len; i++) { 
            char ch = str[i]; 
  
            if (ch != ' ') { 
  
                // forming the word 
                t = t + ch; 
            
  
            // when space is encountered 
            // it means the name is completed 
            // and thereby extracted 
            else
                // printing the first letter 
                // of the name in capital letters 
                Console.Write(Char.ToUpper(t[0]) 
                                + ". "); 
                t = ""
            
        
  
        string temp = ""
  
        // for the surname, we have to print the entire 
        // surname and not just the initial 
        // string "t" has the surname now 
        for (int j = 0; j < t.Length; j++) { 
  
            // first letter of surname in capital letter 
            if (j == 0) 
                temp = temp + Char.ToUpper(t[0]); 
  
            // rest of the letters in small 
            else
                temp = temp + Char.ToLower(t[j]); 
        
  
        // printing surname 
        Console.WriteLine(temp); 
    
  
    public static void Main() 
    
        string str = "ishita bhuiya"
        printInitials(str); 
    
    // This code is contributed by Ryuga

chevron_right


Output:

I. Bhuiya


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.