Program to find the initials of a name.

Given a string name, we have to find the initials of the name

Examples:

Input  : prabhat kumar singh
Output : P K S
We take the first letter of all
words and print in capital letter.

Input  : Jude Law
Output : J L

Input  : abhishek kumar singh
Output : A K S



1) Print first character in capital.
2) Traverse rest of the string and print every character after space in capital letter.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print initials of a name
#include <bits/stdc++.h>
using namespace std;
  
void printInitials(const string& name)
{
    if (name.length() == 0)
        return;
  
    // Since touuper() returns int, we do typecasting
    cout << (char)toupper(name[0]);
  
    // Traverse rest of the string and print the
    // characters after spaces.
    for (int i = 1; i < name.length() - 1; i++)
        if (name[i] == ' ')
            cout << " " << (char)toupper(name[i + 1]);
}
  
// Driver code
int main()
{
    string name = "prabhat kumar singh";
    printInitials(name);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print initials of a name
class initials {
      
    static void printInitials(String name)
    {
        if (name.length() == 0)
            return;
  
        // Since touuper() returns int, 
        // we do typecasting
        System.out.print(Character.toUpperCase(
            name.charAt(0)));
  
        // Traverse rest of the string and 
        // print the characters after spaces.
        for (int i = 1; i < name.length() - 1; i++)
            if (name.charAt(i) == ' ')
                System.out.print(" " + Character.toUpperCase(
                                        name.charAt(i + 1)));
    }
  
    // Driver code
    public static void main(String args[])
    {
        String name = "prabhat kumar singh";
        printInitials(name);
    }
}
  
// This code is contributed by Danish Kaleem

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to print 
# initials of a name
  
# user define function 
def printInitials(name): 
    if(len(name) == 0):
        return
    print(name[0].upper()),
    for i in range(1, len(name) - 1):
        if (name[i] == ' '):
            print (name[i + 1].upper()),
  
def main():
    name = "Prabhat Kumar Singh"
    printInitials(name)
  
if __name__=="__main__":
    main() 
  
# This code is contributed
# by prabhat kumar singh 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print initials of a name
using System;
  
class initials {
      
    static void printInitials(String name)
    {
        if (name.Length == 0)
            return;
  
        // Since touuper() returns int,
        // we do typecasting
        Console.Write(Char.ToUpper(name[0]));
  
        // Traverse rest of the string and 
        // print the characters after spaces.
        for (int i = 1; i < name.Length - 1; i++)
            if (name[i] == ' ')
                Console.Write(" " + Char.ToUpper(name[i + 1]));
    }
  
    // Driver code
    public static void Main()
    {
        String name = "prabhat kumar singh";
        printInitials(name);
    }
}
  
// This code is contributed by nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to print initials of a name
  
function printInitials($name)
{
    if (strlen($name) == 0)
        return;
  
    // Since touuper() returns int, we do typecasting
    echo strtoupper($name[0]);
  
    // Traverse rest of the string and print the
    // characters after spaces.
    for ($i = 1; $i < strlen($name) - 1; $i++)
        if ($name[$i] == ' ')
            echo " " . strtoupper($name[$i + 1]);
}
  
// Driver code
$name = "prabhat kumar singh";
printInitials($name);
  
// This code is contributed by Sam007
?>

chevron_right



Output:

P K S

Another possible solution is given as follows:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print initials of a name
class initials {
      
    static void printInitials(String name)
    {
        if (name.length() == 0)
            return;
  
        //split the string using 'space' 
        //and print the first character of every word
        String words[] = name.split(" ");
        for(String word : words) {
            System.out.print(Character.toUpperCase(word.charAt(0)) + " ");
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        String name = "prabhat kumar singh";
        printInitials(name);
    }
}

chevron_right



Output:

P K S

The complexity of this code will be less than O(w) where w is number of words in sentence, which can be little better than number of characters in String.
This code is contributed by Anuj Khasgiwala

We can also use strtok() function in C/C++ to achieve this.

This article is contributed by Prabhat kumar singh. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


6


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