#include <iostream>
#include <string>
#include <cctype>

// Responsibility: read a line of text from standard input
class ConsoleReader {
public:
    std::string readLine() const {
        std::string input;
        std::cout << "Enter a line of text: ";
        std::getline(std::cin, input);
        return input;
    }
};

// Responsibility: count the vowels (a, e, i, o, u) in the string
class VowelCounter {
public:
    int countVowels(const std::string& text) const {
        int count = 0;
        for (char ch : text) {
            char lower = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
            if (lower == 'a' || lower == 'e' || lower == 'i' || 
                lower == 'o' || lower == 'u') {
                ++count;
            }
        }
        return count;
    }
};

// Responsibility: format the original text and the count into an output message
class ResultFormatter {
public:
    std::string format(const std::string& originalText, int vowelCount) const {
        return "The string \"" + originalText + "\" contains "
               + std::to_string(vowelCount) + " vowels.";
    }
};

// Responsibility: output the message to standard output
class ConsolePrinter {
public:
    void print(const std::string& message) const {
        std::cout << message << std::endl;
    }
};

int main() {
    // Create separate single-responsibility components
    ConsoleReader reader;
    VowelCounter counter;
    ResultFormatter formatter;
    ConsolePrinter printer;

    // Compose them to complete the workflow
    std::string text = reader.readLine();
    int vowels = counter.countVowels(text);
    std::string output = formatter.format(text, vowels);
    printer.print(output);

    return 0;
}

results matching ""

    No results matching ""