1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include <string>
#include <cctype>
#include <utility>
bool is_vowel( char c ) // return true if c is a vowel (non-phonetic)
{
static const std::string vowels = "AEIOUaeiou" ;
return vowels.find(c) != std::string::npos ;
}
std::string to_lower( std::string str )
{
for( char& c : str ) c = std::tolower(c) ;
return str ;
}
// return true if the next three character from position index
// form a three character consonant (currently, only "str")
bool is_consonant3( const std::string& str, std::size_t index )
{ return to_lower( str.substr( index, 3 ) ) == "str" ; }
// return true if the next two character from position index
// form a two character consonant
bool is_consonant2( const std::string& str, std::size_t index )
{
static const std::string consonants[] = { "qu", "tr", "br", "st", "sl", "bl", "cr", "ph", "ch" } ;
const std::string candidate = to_lower( str.substr( index, 2 ) ) ;
for( const auto& cons2 : consonants ) if( candidate == cons2 ) return true ;
return false ;
}
enum type { VOWEL, CONSONANT, NEITHER };
// classify the next component of str, starting at position index
// return a pair with first == type and second == number of characters
std::pair< type, std::size_t > classify( const std::string& str, std::size_t index )
{
// no more characters
if( index == str.size() ) return { NEITHER, 0 } ;
// consonant, 3 characters
if( is_consonant3( str, index ) ) return { CONSONANT, 3 } ;
// consonant, 2 characters
if( is_consonant2( str, index ) ) return { CONSONANT, 2 } ;
// vowel, 1 character
if( is_vowel( str[index] ) ) return { VOWEL, 1 } ;
// alpha, consonant, 1 character
if( std::isalpha( str[index] ) ) return { CONSONANT, 1 } ;
// non-alpha, 1 character
else return { NEITHER, 1 } ;
}
int main()
{
const std::string test_str = "abra(aqusto) trabrbst astrid!" ;
std::size_t index = 0 ;
while( index < test_str.size() )
{
const auto [ ty, n ] = classify( test_str, index ) ;
std::cout << "\n\n" << test_str << '\n' << std::string( index, ' ' ) << std::string( n, '^' )
<< "\n'" << test_str.substr(index,n) << "' " ;
if( ty == VOWEL ) std::cout << "VOWEL\n" ;
else if( ty == CONSONANT ) std::cout << "CONSONANT-" << n << '\n' ;
else std::cout << "NEITHER\n" ;
index += n ;
}
}
|