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
- Print first character in capital.
- Traverse rest of the string and print every character after space in capital letter.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void printInitials( const string& name)
{
if (name.length() == 0)
return ;
cout << ( char ) toupper (name[0]);
for ( int i = 1; i < name.length() - 1; i++)
if (name[i] == ' ' )
cout << " " << ( char ) toupper (name[i + 1]);
}
int main()
{
string name = "prabhat kumar singh" ;
printInitials(name);
return 0;
}
|
Java
import java.io.*;
class initials {
static void printInitials(String name)
{
if (name.length() == 0 )
return ;
System.out.print(Character.toUpperCase(
name.charAt( 0 )));
for ( int i = 1 ; i < name.length() - 1 ; i++)
if (name.charAt(i) == ' ' )
System.out.print( " " + Character.toUpperCase(
name.charAt(i + 1 )));
}
public static void main(String args[])
{
String name = "prabhat kumar singh" ;
printInitials(name);
}
}
|
Python
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()
|
C#
using System;
class initials {
static void printInitials(String name)
{
if (name.Length == 0)
return ;
Console.Write(Char.ToUpper(name[0]));
for ( int i = 1; i < name.Length - 1; i++)
if (name[i] == ' ' )
Console.Write( " " + Char.ToUpper(name[i + 1]));
}
public static void Main()
{
String name = "prabhat kumar singh" ;
printInitials(name);
}
}
|
PHP
<?php
function printInitials( $name )
{
if ( strlen ( $name ) == 0)
return ;
echo strtoupper ( $name [0]);
for ( $i = 1; $i < strlen ( $name ) - 1; $i ++)
if ( $name [ $i ] == ' ' )
echo " " . strtoupper ( $name [ $i + 1]);
}
$name = "prabhat kumar singh" ;
printInitials( $name );
?>
|
Javascript
<script>
function printInitials(name)
{
if (name.length == 0)
return ;
document.write(name[0].toUpperCase());
for (let i = 1; i < name.length - 1; i++)
if (name[i] == ' ' )
document.write( " " + name[i + 1].toUpperCase());
}
let name = "prabhat kumar singh" ;
printInitials(name);
</script>
|
Time Complexity: O(n), Here n is the length of the string.
Auxiliary Space: O(1), As constant extra space is used.
Another possible solution is given as follows:
C++
#include <bits/stdc++.h>
using namespace std;
void printInitials(string name)
{
if (name.length() == 0)
return ;
stringstream X(name);
while (getline(X, name, ' ' )) {
cout << ( char ) toupper (name[0])<< " " ;
}
}
int main()
{
string name = "prabhat kumar singh" ;
printInitials(name);
return 0;
}
|
Java
import java.io.*;
class initials {
static void printInitials(String name)
{
if (name.length() == 0 )
return ;
String words[] = name.split( " " );
for (String word : words) {
System.out.print(Character.toUpperCase(word.charAt( 0 )) + " " );
}
}
public static void main(String args[])
{
String name = "prabhat kumar singh" ;
printInitials(name);
}
}
|
Python3
def printInitials(name):
if ( len (name) = = 0 ):
return
words = name.split( " " )
for word in words:
print (word[ 0 ].upper(), end = " " )
if __name__ = = '__main__' :
name = "prabhat kumar singh"
printInitials(name)
|
C#
using System;
public class initials {
static void printInitials(String name)
{
if (name.Length == 0)
return ;
String []words = name.Split( ' ' );
foreach (String word in words) {
Console.Write( char .ToUpper(word[0]) + " " );
}
}
public static void Main(String []args)
{
String name = "prabhat kumar singh" ;
printInitials(name);
}
}
|
Javascript
<script>
function printInitials( name) {
if (name.length == 0)
return ;
var words = name.split( " " );
words.forEach(myFunction);
}
function myFunction(item) {
document.write((item[0].toUpperCase()) + " " );
}
var name = "prabhat kumar singh" ;
printInitials(name);
</script>
|
The time complexity of this code will be less than O(N) where N is number of words in sentence, which can be little better than number of characters in String.
Space Complexity: O(N), where N is the length of the string. We need to store the entire string in the stringstream object.
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 neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!