首页 > 解决方案 > 仅在 C++ 中使用递归删除元音和特殊字符,同时对辅音进行修饰

问题描述

我正在尝试编写一个递归函数,从短语中删除元音、特殊字符和空格并将辅音大写。我很难想出一个纯粹的递归函数。也不知道如何在 C++ 中链接函数。可以toUpperCase()isalpha()一起使用吗?有没有办法将子句中的两个条件结合起来else if放在else子句中?

bool isVowel(char ch) {

        if (ch == 'A' || ch == 'E' || ch == 'I' ||
            ch == 'O' || ch == 'U' || ch == 'Y') {
        return true;
    }
        else {
        return false;
    }
}

string onlyConnectize(string phrase) {        

        phrase = toUpperCase(phrase);

        // base case
        if (phrase.empty()) {
        return "";
    }
        else if (!isalpha(phrase[0])) {
        return "";
    }
        else if (isVowel(phrase[0])) {
        return "";
    }
        else {
        return phrase[0] + onlyConnectize(phrase.substr(1));
    }
}

标签: c++recursion

解决方案


推荐阅读