Program to print the initials of a name with the surname
Last Updated :
23 Nov, 2023
Given a full name in the form of a string, the task is to print the initials of a name, in short, and the 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++
#include <bits/stdc++.h>
using namespace std;
void printInitials(string str)
{
int len = str.length();
str.erase(0, str.find_first_not_of( ' ' ));
str.erase(str.find_last_not_of( ' ' ) + 1);
string t = "" ;
for ( int i = 0; i < len; i++)
{
char ch = str[i];
if (ch != ' ' )
t = t + ch;
else
{
cout << ( char ) toupper (t[0]) << ". " ;
t = "" ;
}
}
string temp = "" ;
for ( int j = 0; j < t.length(); j++)
{
if (j == 0) temp = temp + ( char ) toupper (t[0]);
else
temp = temp + ( char ) tolower (t[j]);
}
cout << temp << endl;
}
int main()
{
string str = "ishita bhuiya" ;
printInitials(str);
}
|
Java
import java.util.*;
class Initials {
public static void printInitials(String str)
{
int len = str.length();
str = str.trim();
String t = "" ;
for ( int i = 0 ; i < len; i++) {
char ch = str.charAt(i);
if (ch != ' ' ) {
t = t + ch;
}
else {
System.out.print(Character.toUpperCase(t.charAt( 0 ))
+ ". " );
t = "" ;
}
}
String temp = "" ;
for ( int j = 0 ; j < t.length(); j++) {
if (j == 0 )
temp = temp + Character.toUpperCase(t.charAt( 0 ));
else
temp = temp + Character.toLowerCase(t.charAt(j));
}
System.out.println(temp);
}
public static void main(String[] args)
{
String str = "ishita bhuiya" ;
printInitials(str);
}
}
|
Python3
def printInitials(string: str ):
length = len (string)
string.strip()
t = ""
for i in range (length):
ch = string[i]
if ch ! = ' ' :
t + = ch
else :
print (t[ 0 ].upper() + ". " , end = "")
t = ""
temp = ""
for j in range ( len (t)):
if j = = 0 :
temp + = t[ 0 ].upper()
else :
temp + = t[j].lower()
print (temp)
if __name__ = = "__main__" :
string = "ishita bhuiya"
printInitials(string)
|
C#
using System;
class Initials {
public static void printInitials( string str)
{
int len = str.Length ;
str = str.Trim();
String t = "" ;
for ( int i = 0; i < len; i++) {
char ch = str[i];
if (ch != ' ' ) {
t = t + ch;
}
else {
Console.Write(Char.ToUpper(t[0])
+ ". " );
t = "" ;
}
}
string temp = "" ;
for ( int j = 0; j < t.Length; j++) {
if (j == 0)
temp = temp + Char.ToUpper(t[0]);
else
temp = temp + Char.ToLower(t[j]);
}
Console.WriteLine(temp);
}
public static void Main()
{
string str = "ishita bhuiya" ;
printInitials(str);
}
}
|
Javascript
<script>
function printInitials(string){
let Length = string.length
string = string.trim()
let t = ""
for (let i=0;i<Length;i++){
let ch = string[i]
if (ch != ' ' )
t += ch
else {
document.write(t[0].toUpperCase() + ". " , "" )
t = ""
}
}
let temp = ""
for (let j=0;j<t.length;j++){
if (j == 0)
temp += t[0].toUpperCase()
else
temp += t[j].toLowerCase()
}
document.write(temp, "</br>" )
}
let string = "ishita bhuiya"
printInitials(string)
</script>
|
Time complexity: O(n), Here n is the length of the string.
Auxiliary space: O(1), As constant extra space is used.
Approach:
- Split the name using the space delimiter.
- Get the first character of each name part and append a period to it.
- Combine the initials and the last name using a space.
- Print the result.
C++
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string name = "Uppala Omkhar" ;
std::vector<std::string> name_parts;
std::string part;
for ( char c : name) {
if (c == ' ' ) {
name_parts.push_back(part);
part = "" ;
} else {
part += c;
}
}
name_parts.push_back(part);
std::string initials = "" ;
for ( int i = 0; i < name_parts.size() - 1; i++) {
initials += toupper (name_parts[i][0]);
initials += ". " ;
}
std::string surname = name_parts.back();
surname[0] = toupper (surname[0]);
std::cout << initials << surname << std::endl;
return 0;
}
|
Java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String name = "Uppala Omkhar" ;
String[] nameParts = name.split( " " );
ArrayList<String> namePartsList = new ArrayList<>();
for (String part : nameParts) {
namePartsList.add(part);
}
StringBuilder initials = new StringBuilder();
for ( int i = 0 ; i < namePartsList.size() - 1 ; i++) {
initials.append(Character.toUpperCase(namePartsList.get(i).charAt( 0 ))).append( ". " );
}
String surname = namePartsList.get(namePartsList.size() - 1 );
surname = Character.toUpperCase(surname.charAt( 0 )) + surname.substring( 1 );
System.out.println(initials.toString() + surname);
}
}
|
Python3
name = "Uppala Omkhar"
name_parts = name.split()
initials = ""
for part in name_parts[: - 1 ]:
initials + = part[ 0 ].upper() + ". "
surname = name_parts[ - 1 ].capitalize()
print (initials + surname)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string name = "Uppala Omkhar" ;
List< string > nameParts = new List< string >();
string part = "" ;
foreach ( char c in name)
{
if (c == ' ' )
{
nameParts.Add(part);
part = "" ;
}
else
{
part += c;
}
}
nameParts.Add(part);
string initials = "" ;
for ( int i = 0; i < nameParts.Count - 1; i++)
{
initials += char .ToUpper(nameParts[i][0]) + ". " ;
}
string surname = nameParts[nameParts.Count - 1];
surname = char .ToUpper(surname[0]) + surname.Substring(1);
Console.WriteLine(initials + surname);
}
}
|
Javascript
const name = "Uppala Omkhar" ;
const nameParts = [];
let part = "" ;
for (const char of name) {
if (char === ' ' ) {
nameParts.push(part);
part = "" ;
} else {
part += char;
}
}
nameParts.push(part);
let initials = "" ;
for (let i = 0; i < nameParts.length - 1; i++) {
initials += nameParts[i][0].toUpperCase() + ". " ;
}
let surname = nameParts[nameParts.length - 1];
surname = surname[0].toUpperCase() + surname.slice(1);
console.log(initials + surname);
|
Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(1), as we are not using any extra data structure.
Share your thoughts in the comments
Please Login to comment...