Task: Find words which contain the most consonants


C++: #include <bitset>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>



size_t consonants(const std::string& word) {
    std::bitset<26> bits;
    size_t bit = 0;
    for (char ch : word) {
        ch = std::tolower(static_cast<unsigned char>(ch));
        if (ch < 'a' || ch > 'z')
            continue;
        switch (ch) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            break;
        default:
            bit = ch - 'a';
            if (bits.test(bit))
                return 0;
            bits.set(bit);
            break;
        }
    }
    return bits.count();
}

int main(int argc, char** argv) {
    const char* filename(argc < 2 ? "unixdict.txt" : argv[1]);
    std::ifstream in(filename);
    if (!in) {
        std::cerr << "Cannot open file '" << filename << "'.\n";
        return EXIT_FAILURE;
    }
    std::string word;
    std::map<size_t, std::vector<std::string>, std::greater<int>> map;
    while (getline(in, word)) {
        if (word.size() <= 10)
            continue;
        size_t count = consonants(word);
        if (count != 0)
            map[count].push_back(word);
    }
    const int columns = 4;
    for (const auto& p : map) {
        std::cout << p.first << " consonants (" << p.second.size() << "):\n";
        int n = 0;
        for (const auto& word : p.second) {
            std::cout << std::left << std::setw(18) << word;
            ++n;
            if (n % columns == 0)
                std::cout << '\n';
        }
        if (n % columns != 0)
            std::cout << '\n';
        std::cout << '\n';
    }
    return EXIT_SUCCESS;
}










Ada: with Ada.Text_Io;
with Ada.Strings.Fixed;
with Ada.Containers.Indefinite_Vectors;

procedure Most_Consonants is
   use Ada.Text_Io;

   package String_Vectors is
     new Ada.Containers.Indefinite_Vectors (Index_Type   => Positive,
                                            Element_Type => String);

   function Is_Vowel (Letter : Character) return Boolean
   is (Ada.Strings.Fixed.Index ("aeiou", "" & Letter) /= 0);

   type Consonant_Count is range 0 .. 15;
   Filename : constant String := "unixdict.txt";
   File     : File_Type;
   Words    : array (Consonant_Count) of String_Vectors.Vector;
begin
   Open (File, In_File, Filename);
   while not End_Of_File (File) loop
      declare
         Word  : constant String  := Get_Line (File);
         Found : array (Character) of Boolean := (others => False);
         Conso : Consonant_Count := 0;
         Bad   : Boolean         := False;
      begin
         for Letter of Word loop
            if Is_Vowel (Letter) then
               null;
            elsif Found (Letter) then
               Bad := True;
            else
               Found (Letter) := True;
               Conso := Conso + 1;
            end if;
         end loop;
         if not Bad and Word'Length > 10 then
            Words (Conso).Append (Word);
         end if;
      end;
   end loop;
   Close (File);


   for Cons in Words'Range loop
      if not Words (Cons).Is_Empty then
         Put (Cons'Image & " consonants: ");
         Put (Words (Cons).Length'Image & " words");
         New_Line;
      end if;
      declare
         Column : Natural := 0;
         Image  : String (1 .. 15);
      begin
         for Word of Words (Cons) loop
            Ada.Strings.Fixed.Move (Word, Image);
            Put (Image);
            Column := Column + 1;
            if Column mod 8 = 0 then
               New_Line;
            end if;
         end loop;
      end;
      if not Words (Cons).Is_Empty then
         New_Line;
      end if;
   end loop;

end Most_Consonants;