简体   繁体   English

如何使用string :: find()?

[英]How to use string::find()?

Print "Censored" if userInput contains the word "darn", else print userInput. 如果userInput包含单词“ darn”,则打印“ Censored”,否则打印userInput。 End with newline. 以换行符结尾。 Hint: find() returns string::npos if the item being searched for is not found. 提示:如果未找到要搜索的项目,则find()返回string :: npos。

Note: These activities may test code with different test values. 注意:这些活动可能会使用不同的测试值来测试代码。 This activity will perform three tests, with userInput of "That darn cat.", then with "Dang, that was scary!", then with "I'm darning your socks." 此活动将执行三个测试,使用userInput输入“那只该死的猫。”,然后输入“当当,那太可怕了!”,然后输入“我正在织袜子”。

Here is the code I tried. 这是我尝试的代码。 I'm really not sure what else to try. 我真的不确定还有什么尝试。

#include <iostream>
#include <string>
using namespace std;

int main() {
   string userInput;

   userInput = "That darn cat.";

   if (userInput.find("darn")) {
      cout << "Censored" << endl;
   }

   else {
      cout << userInput << endl; 
   }

   return 0;
}

userInput should result in Censored if it contains "darn" . 如果userInput包含"darn"则应导致Censored Otherwise, it should print the userInput . 否则,它应该打印userInput

My results say Censored for every input. 我的结果说,每个输入都Censored

Your are not following the instructions you were given. 您没有按照您的指示进行操作。

Specifically, you are missing code for these conditions: 具体来说,您缺少以下条件的代码:

  • Hint: find() returns string::npos if the item being searched for is not found. 提示:如果未找到要搜索的项目,则find()返回string :: npos。

    You are not checking the return value of find() for npos (which is defined as string::size_type(-1) ). 您不检查npos (定义为string::size_type(-1) )的find()返回值。

    find() returns a numeric value for the index of the found substring, or npos if not found. find()返回找到的子字符串的索引的数字值;如果找不到,则npos

    The statement if (userInput.find("darn")) is checking for a zero vs non-zero index value. 语句if (userInput.find("darn"))正在检查零索引值与非零索引值。 In all three test cases, find() will not return index 0, so any non-zero value will cause the if statement to evaluate as true and the "Censored" block will be entered. 在所有三个测试用例中, find()都不会返回索引0,因此任何非零值都将导致if语句的评估结果为true并将输入"Censored"块。

  • This activity will perform three tests, with userInput of "That darn cat.", then with "Dang, that was scary!", then with "I'm darning your socks." 此活动将执行三个测试,使用userInput输入“那只该死的猫。”,然后输入“当当,那太可怕了!”,然后输入“我正在织袜子”。

    You are performing only the 1st test but not the other tests. 您仅执行第一项测试,而不执行其他测试。

Try this instead: 尝试以下方法:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string userInput;

    userInput = "That darn cat.";

    if (userInput.find("darn") != string::npos) {
        cout << "Censored" << endl;
    }
    else {
        cout << userInput << endl;
    }

    userInput = "Dang, that was scary!";

    if (userInput.find("darn") != string::npos) {
        cout << "Censored" << endl;
    }
    else {
        cout << userInput << endl;
    }

    userInput = "I'm darning your socks.";

    if (userInput.find("darn") != string::npos) {
        cout << "Censored" << endl;
    }
    else {
        cout << userInput << endl;
    }

    return 0;
}

Which can then be rewritten using a reusable function: 然后可以使用可重用的函数将其重写:

#include <iostream>
#include <string>
using namespace std;

void checkInput(const string &input) {
    if (input.find("darn") != string::npos) {
        cout << "Censored" << endl;
    }
    else {
        cout << input << endl;
    }
}

int main() {
    string userInput;

    userInput = "That darn cat.";
    checkInput(userInput);

    userInput = "Dang, that was scary!";
    checkInput(userInput);

    userInput = "I'm darning your socks.";
    checkInput(userInput);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM