#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cctype>
#include <algorithm>

std::string make_key( std::string str )
{
    // remove non-alphabets
    static const auto is_not_alpha = [] ( char c ) { return !std::isalpha(c) ; } ;
    str.erase( std::remove_if( str.begin(), str.end(), is_not_alpha ), str.end() ) ;

    // convert to lower case
    for( char& c : str ) c = std::tolower(c) ;

    // and sort
    std::sort( str.begin(), str.end() ) ;

    return str ;
}

using anagram_map = std::map< std::string, std::vector<std::string> > ;

anagram_map make_anagram_map( std::istream& stm )
{
    anagram_map ana_map ;

    std::string word ;
    while( stm >> word ) ana_map[ make_key(word) ].push_back(word) ;

    return ana_map ;
}

void look_up( const std::string& word, const anagram_map& ana_map )
{
    auto iter = ana_map.find( make_key(word) ) ;
    if( iter != ana_map.end() )
    {
        std::cout << "anagrams of '" << word << "':\n" ;
        for( const auto& str : iter->second ) std::cout << "    " << str << '\n' ;
    }
    else std::cout << "no anagrams of '" << word << "' were found\n" ;
}

#include <sstream>

int main()
{
    std::istringstream stm( "Petal Maple Teal leapt!! Lepta tale Pleat ample LATE? Plate" ) ;
    const anagram_map ana_map = make_anagram_map(stm) ;

    look_up( "petal", ana_map ) ;
    look_up( "late", ana_map ) ;
    look_up( "quick", ana_map ) ;
}
