C++ Program to Find Initials of a Name Last Updated : 27 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string name, we have to find the initials of the name Examples: Input: Kamlesh Joshi Output: K J We take the first letter of all words and print in capital letter. Input: Jude Law Output: J L Input: Abhishek Kumar Bisht Output: A K B 1) Print first character in capital. 2) Traverse rest of the string and print every character after space in capital letter. C++ // 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 toupper() 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 = "Kamlesh Joshi"; printInitials(name); return 0; } Output: K J 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++ // C++ program to solve the // above approach #include <bits/stdc++.h> using namespace std; void printInitials(string name) { if (name.length() == 0) return; // split the string using 'space' // and print the first character of // every word // X is an object of stringstream // that references the S string stringstream X(name); // use while loop to check the // getline() function condition while (getline(X, name, ' ')) { /* X represents to read the string from stringstream, T use for store the token string and, ' ' whitespace represents to split the string where whitespace is found. */ // Print split string cout << (char)toupper(name[0]) << " "; } } // Driver code int main() { string name = "Kamlesh Joshi"; printInitials(name); return 0; } Output: K J Time complexity: O(w), 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. We can also use strtok() function in C/C++ to achieve this. Auxiliary space: O(1). Comment More infoAdvertise with us Next Article C++ Program to Find Initials of a Name K kartik Follow Improve Article Tags : C++ Programs C++ C Misc Programs Practice Tags : CPP Similar Reads C++ Program to Implement strpbrk() Function strpbrk() is a string function in C++ STL that takes in two strings and finds the first occurrence of any character of string2 in string1. This function returns the pointer to the character of string2 in string1 if there is any, otherwise returns NULL. Syntax: char* strpbrk(const char *str1, const c 3 min read C++ Program To Find Longest Common Prefix Using Sorting Problem Statement: Given a set of strings, find the longest common prefix.Examples: Input: {"geeksforgeeks", "geeks", "geek", "geezer"} Output: "gee" Input: {"apple", "ape", "april"} Output: "ap" The longest common prefix for an array of strings is the common prefix between 2 most dissimilar strings 2 min read Implementing of strtok() function in C++ The strtok() function is used in tokenizing a string based on a delimiter. It is present in the header file "string.h" and returns a pointer to the next token if present, if the next token is not present it returns NULL. To get all the tokens the idea is to call this function in a loop. Header File: 3 min read C Program to Extract Characters From a String Character extraction can be done by iterating through the string in the form of a character array. It basically means plucking out a certain amount of characters from an array or a string. Now, to take input in C we do it by using the following methods: scanf("%c",&str[i]); - Using a loopscanf(" 2 min read How to Match a Pattern in a String in C++? In C++, strings are sequences of characters stored in a char array. Matching a pattern in a string involves searching for a specific sequence of characters (the pattern) within a given string. In this article, we will learn how to match a pattern in a string in C++. Example: Input:Text: "GeeksForGee 2 min read Like