Daily Challenge #44

Secret Society

A group of friends have decided to start a secret society. The name will be the first letter of each of their names, sorted in alphabetical order.

Create a function that takes in an array of names and returns the name of the secret society.

Examples

societyName(["Adam", "Sarah", "Malcolm"]) ➞ "AMS"

societyName(["Harry", "Newt", "Luna", "Cho"]) ➞ "CHLN"

societyName(["Phoebe", "Chandler", "Rachel", "Ross", "Monica", "Joey"]) ➞ "CJMPRR"

Notes

The secret society’s name should be entirely uppercased.

6 Likes


There are some errors in the solution which needs to be rectified

4 Likes

But as @Peter has wrote:

Names are in array which is also a kind of list.
you will receive a list and have to do further process.
So in for loop directly use names list/array.

3 Likes

I have JAVA code so if anyone wants to convert it to blocks then,

    int n;
    String temp;
    Scanner s = new Scanner(System.in);
    System.out.print("Enter number of names you want to enter:");
    n = s.nextInt();
    String[] names = new String[n];
    Scanner s1 = new Scanner(System.in);
    System.out.println("Enter all the names:");
    for(int i = 0; i < n; i++)
    {
        names[i] = s1.nextLine();
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (names[i].compareTo(names[j])>0)
            {
                temp = names[i];
                names[i] = names[j];
                names[j] = temp;
            }
        }
    }
    System.out.print("Names in Sorted Order:");
    for (int i = 0; i < n - 1; i++)
    {
        System.out.print(names[i] + ",");
    }
    System.out.print(names[n - 1]);