Program to print the initials of a name with the surname

13 Jun, 2022
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: 
C++
Java
Python3
C#
Javascript
// 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 Codeint main(){    string str = "ishita bhuiya";    printInitials(str);} // This code is contributed by// sanjeev2552
// Java program to print the initials// of a name with the surnameimport 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);    }}
# Python program to print the initials# of a name with the surnamedef 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 Codeif __name__ == "__main__":     string = "ishita bhuiya"    printInitials(string) # This code is contributed by# sanjeev2552
// C# program to print the initials// of a name with the surnameusing 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}
<script> // JavaScript program to print the initials// of a name with the surnamefunction printInitials(string){    let Length = string.length     // to remove any leading or trailing spaces    string = string.trim()     // to store extracted words    let t = ""    for(let i=0;i<Length;i++){        let 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            document.write(t[0].toUpperCase() + ". ","")            t = ""        }    }     let temp = ""     // for the surname, we have to print the entire    // surname and not just the initial    // string "t" has the surname now    for(let j=0;j<t.length;j++){         // first letter of surname in capital letter        if(j == 0)            temp += t[0].toUpperCase()         // rest of the letters in small        else            temp += t[j].toLowerCase()    }     // printing surname    document.write(temp,"</br>")} // Driver Code let string = "ishita bhuiya"printInitials(string)  // this code is contributed by shinjanpatra </script>
I. Bhuiya
ankthon
sanjeev2552
shinjanpatra
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.