📜  查找姓名首字母的程序。

📅  最后修改于: 2022-05-13 01:57:08.355000             🧑  作者: Mango

查找姓名首字母的程序。

给定一个字符串名称,我们必须找到名称的首字母

例子:

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)以大写字母打印第一个字符。
2)遍历字符串的其余部分并以大写字母打印空格后的每个字符。

C++
// C++ program to print initials of a name
#include 
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 = "prabhat kumar singh";
    printInitials(name);
    return 0;
}


Java
// Java program to print initials of a name
class initials {
     
    static void printInitials(String name)
    {
        if (name.length() == 0)
            return;
 
        // Since toupper() 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


Python
# 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


C#
// C# program to print initials of a name
using System;
 
class initials {
     
    static void printInitials(String name)
    {
        if (name.Length == 0)
            return;
 
        // Since toupper() 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.


PHP


Javascript


C++
// C++ program to solve the above approach
#include 
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
    stringstream X(name); // X is an object of stringstream
                          // that references the S string
 
    // 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. */
 
        cout << (char)toupper(name[0])<<" "; // print split string
    }
}
 
// Driver code
int main()
{
    string name = "prabhat kumar singh";
    printInitials(name);
    return 0;
}
 
// This code is contributed by gauravrajput1


Java
// 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);
    }
}


Python3
# Python3 program to print initials of a name
def printInitials(name):
     
    if (len(name) == 0):
        return
 
    # Split the string using 'space'
    # and print the first character of
    # every word
    words = name.split(" ")
    for word in words:
        print(word[0].upper(), end = " ")
 
# Driver code
if __name__ == '__main__':
     
    name = "prabhat kumar singh"
    printInitials(name)
 
# This code is contributed by mohit kumar 29


C#
// C# program to print initials of a name
using System;
 
public 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(' ');
        foreach(String word in words) {
            Console.Write(char.ToUpper(word[0]) + " ");
        }
    }
 
    // Driver code
    public static void Main(String []args)
    {
        String name = "prabhat kumar singh";
        printInitials(name);
    }
}
 
// This code is contributed by gauravrajput1


Javascript


输出:

P K S

另一种可能的解决方案如下:

C++

// C++ program to solve the above approach
#include 
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
    stringstream X(name); // X is an object of stringstream
                          // that references the S string
 
    // 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. */
 
        cout << (char)toupper(name[0])<<" "; // print split string
    }
}
 
// Driver code
int main()
{
    string name = "prabhat kumar singh";
    printInitials(name);
    return 0;
}
 
// This code is contributed by gauravrajput1

Java

// 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);
    }
}

Python3

# Python3 program to print initials of a name
def printInitials(name):
     
    if (len(name) == 0):
        return
 
    # Split the string using 'space'
    # and print the first character of
    # every word
    words = name.split(" ")
    for word in words:
        print(word[0].upper(), end = " ")
 
# Driver code
if __name__ == '__main__':
     
    name = "prabhat kumar singh"
    printInitials(name)
 
# This code is contributed by mohit kumar 29

C#

// C# program to print initials of a name
using System;
 
public 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(' ');
        foreach(String word in words) {
            Console.Write(char.ToUpper(word[0]) + " ");
        }
    }
 
    // Driver code
    public static void Main(String []args)
    {
        String name = "prabhat kumar singh";
        printInitials(name);
    }
}
 
// This code is contributed by gauravrajput1

Javascript


输出:

P K S

此代码的复杂度将小于 O(w),其中 w 是句子中的单词数,这可能比 String 中的字符数好一点。
此代码由 Anuj Khasgiwala 提供
我们也可以使用 C/C++ 中的 strtok()函数来实现这一点。