检查一个字符串是否仅包含另一个字符串中的字符 [英] Check if a string contains only the characters in another string

查看:77
本文介绍了检查一个字符串是否仅包含另一个字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,确定输入的单词的所有字母是否包含在另一个可接受的字符串中.

I want to write a function that determines if all the letters of an inputted word are contained in another string of acceptable letters.

bool ContainsOnly(std::string inputtedWord, std::string acceptableLetters)
{
    // ... how do I write this?
}

这是我的测试框架:

bool Tester(std::string inputtedWord, std::string acceptableLetters)
{
    if (ContainsOnly(inputtedWord, acceptableLetters)) {
        std::cout << "Good!" << std::endl;
        return true;
    }
    else {
        std::cout << "No good!" << std::endl;
        return false;
    }
}

int main()
{
    std::string acceptableLetters;
    std::string inputtedWord;

    std::cout << "Please input the acceptable letters in your words: " << std::endl;
    std::cin >> acceptableLetters;

    while (inputtedWord != "STOP") 
    {
        std::cout << "Please input the word you would like to test: (type STOP to end testing): " << std::endl;
        std::cin >> inputtedWord;
        Tester(inputtedWord, acceptableLetters);
    }
    return 0;
}

我想要以下输出:

请在您的单词中输入可接受的字母:编码

Please input the acceptable letters in your words: CODING

请输入您要测试的单词:(输入STOP结束测试):COIN

Please input the word you would like to test: (type STOP to end testing): COIN

好!

请输入您要测试的词:(输入STOP结束测试):COP

Please input the word you would like to test: (type STOP to end testing): COP

不好!

推荐答案

您可以使用这是演示.

这篇关于检查一个字符串是否仅包含另一个字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆