w3resource

C#: Longest abecedarian word in a array of words

C# Sharp String: Exercise-64 with Solution

Abecedarian Word:
The English noun and adjective abecedarian has several closely related senses. As a noun, it means "someone learning the letters of the alphabet," and more loosely, "a beginner in a field of learning." As an adjective, abecedarian means "pertaining to the alphabet; arranged in alphabetical order; elementary, rudimentary."

Write a C# Sharp program to find the longest abecedarian word in a given array of words.

Sample Data:
({"abc", "abcd", "abcdef", "pqrstuv"}) -> "pqrstuv"
({"abc", "abcd", "abcdef", "pqrs"}) -> "abcdef"
({}) -> "Empty array!"

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] words = {"abc", "abcd", "abcdef", "pqrstuv"};
            Console.WriteLine("Original array of words: ");
            Console.WriteLine($"{string.Join(", ", words)}");
            Console.WriteLine("Longest abecedarian word in the said array of words: " + test(words));
            string[] words1 = {"abc", "abcd", "abcdef", "pqrs"};
            Console.WriteLine("\nOriginal array of words: ");
            Console.WriteLine($"{string.Join(", ", words1)}");
            Console.WriteLine("Longest abecedarian word in the said array of words: " + test(words1));
            string[] words2 = {};
            Console.WriteLine("\nOriginal array of words: ");
            Console.WriteLine($"{string.Join(", ", words2)}");
            Console.WriteLine("Longest abecedarian word in the said array of words: " + test(words2));
        }

        public static string test(string[] array_words)
        {
            if (!array_words.Any())
            {
                return "Empty array!";
            }
            string result = "";

            foreach (string word in array_words)
            {
                char[] temp = word.ToCharArray();
                Array.Sort(temp);

                string cword = new string(temp);
                if (cword == word && word.Length > result.Length)
                    result = word;
            }

            return result;
        }

    }
}

Sample Output:

Original array of words: 
abc, abcd, abcdef, pqrstuv
Longest abecedarian word in the said array of words: pqrstuv

Original array of words: 
abc, abcd, abcdef, pqrs
Longest abecedarian word in the said array of words: abcdef

Original array of words: 

Longest abecedarian word in the said array of words: Empty array!

Flowchart :

Flowchart: C# Sharp Exercises - Longest abecedarian word in a array of words.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Number of times a substring appeared in a string.
Next C# Sharp Exercise: Find the century from a given year.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.