L6_T1

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.4 kB
5
Indexable
Never
// Example program
#include <iostream>
#include <string>

const int STRLEN = 20;

using namespace std;


bool CharBelongsTo(char ch, char str[], int n);
int SupportDividorsCount(char[], int);

int main()
{
    char *str = new char[STRLEN];
    
    cout << "Input string: " << endl;
    cin.getline(str, STRLEN); 
    
    cout << str;
    
    cout << endl;
    
    cout << SupportDividorsCount(str, STRLEN);
    
    delete []str;
}
int SupportDividorsCount(char *str, int n)
{
    char consonants[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'};
    int consonantsCount = sizeof(consonants) / sizeof(char);
    
    char dividors[] = {'-', ' '};
    int dividorsCount = sizeof(dividors) / sizeof(char);
 
    int suppDivCnt = 0;
    
    for(int i = 2; str[i] != '\0'; i++)
    {
        //cout << str[i - 2] << str[i - 1] << str[i] << endl;
        if(CharBelongsTo(str[i - 2], consonants, consonantsCount) 
            && CharBelongsTo(str[i - 1], dividors, dividorsCount)
            && CharBelongsTo(str[i], consonants, consonantsCount))
        {
            suppDivCnt++;    
        }
    }
    return suppDivCnt;
}
bool CharBelongsTo(char ch, char str[], int n)
{
    bool belongsTo = false;
    
    for(int i = 0; str[i] != '\0'; i++)
    {
        if(str[i] == ch)
        {
            belongsTo = true;
            break;
        }
    }  
    return belongsTo;
}