Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Program to print the initials of a name with the surname

  • Last Updated : 28 Jan, 2020

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:

Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII

Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!

 



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:

C++




// C++ program to print the initials
// of a name with the surname
#include <bits/stdc++.h>
using namespace std;
  
void printInitials(string str) 
{
    int len = str.length();
  
    // to remove any leading or trailing spaces
    str.erase(0, str.find_first_not_of(' '));
    str.erase(str.find_last_not_of(' ') + 1);
  
    // 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
            cout << (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
    cout << temp << endl;
}
  
// Driver Code
int main() 
{
    string str = "ishita bhuiya";
    printInitials(str);
}
  
// This code is contributed by
// sanjeev2552

Java




// 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);
    }
}

Python3




# Python program to print the initials
# of a name with the surname
def printInitials(string: str):
    length = len(string)
  
    # to remove any leading or trailing spaces
    string.strip()
  
    # to store extracted words
    t = ""
    for i in range(length):
        ch = string[i]
        if ch != ' ':
  
            # forming the word
            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
            print(t[0].upper() + ". ", end="")
            t = ""
  
    temp = ""
  
    # for the surname, we have to print the entire
    # surname and not just the initial
    # string "t" has the surname now
    for j in range(len(t)):
  
        # first letter of surname in capital letter
        if j == 0:
            temp += t[0].upper()
  
        # rest of the letters in small
        else:
            temp += t[j].lower()
  
    # printing surname
    print(temp)
  
# Driver Code
if __name__ == "__main__":
  
    string = "ishita bhuiya"
    printInitials(string)
  
# This code is contributed by
# sanjeev2552

C#




// 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
Output:
I. Bhuiya



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!