Given a string name, we have to find the initials of the name
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.
P K S
Another possible solution is given as follows:
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 email@example.com. 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.
- A Program to check if strings are rotations of each other or not
- Write a program to print all permutations of a given string
- Given a string, find its first non-repeating character
- Write a program to reverse an array or string
- Find the smallest window in a string containing all characters of another string
- Given a number, find the next smallest palindrome
- Program to validate an IP address
- Find if a string is interleaved of two other strings | DP-33
- Find the first non-repeating character from a stream of characters
- Find Excel column name from a given column number
- Given a sorted dictionary of an alien language, find order of characters
- Find if an array of strings can be chained to form a circle | Set 1
- Given two strings, find if first string is a subsequence of second
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Find all distinct palindromic sub-strings of a given string