How to use string::find()?

Asked by At

Print "Censored" if userInput contains the word "darn", else print userInput. End with newline. Hint: find() returns string::npos if the item being searched for is not found.

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."

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". Otherwise, it should print the userInput.

My results say Censored for every input.

1 Answers

1
Remy Lebeau On

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.

    You are not checking the return value of find() for npos (which is defined as string::size_type(-1)).

    find() returns a numeric value for the index of the found substring, or npos if not found.

    The statement if (userInput.find("darn")) is checking for a zero vs non-zero index value. 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.

  • 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."

    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;
}