MHB C ++ typing censored for 'darn'

  • Thread starter Thread starter adrumheiser
  • Start date Start date
Click For Summary
The discussion revolves around a C++ coding issue where the user struggles to correctly censor the word "darn" in a string input. The original code incorrectly uses the `find` method, leading to unintended behavior where "Censored" is printed even when "darn" is not present. The key point of the solution is to check if the result of `userInput.find("darn")` is not equal to `string::npos` (which indicates that the substring was not found). The corrected condition should be `if (userInput.find("darn") != string::npos)`, ensuring that "Censored" is only printed when "darn" is actually in the input. The user confirms that this modification resolves the issue.
adrumheiser
Messages
2
Reaction score
0
Hi all, I am new to c++ and need help with my homework. I have tried many times and somehow cannot get it right. My following code works for 'darn' but when tested with "Dang, that was scary!," prints censored also. Any hints?

Print "Censored" if userInput contains the word "darn", else print userInput. End with newline.

#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;
}
 
Technology news on Phys.org
Hi,
Your problem is in the statement (BTW use the code tag to make your code readable):
Code:
if (userInput.find("darn")) {
   cout << "Censored" << endl;
}
else {
   cout << userInput << endl;
}
As in all of C++, if the expression userInput.find("darn") has a non-zero value, the if statement is deemed to be true. Now the return value of string's find method is the position (index) of the first occurrence of parameter target in case target is actually a substring; this can range from 0 to the length of the string-1. If target is not a substring, the return value is string::npos. This last value is actually -1, which is non-zero. So in your construct userInput.find("darn") is always true unless "darn" is the first few characters of userInput. This is definitely not what you want. So modify your if statement with
Code:
if (userInput.find("darn") != -1) {
   cout << "Censored" << endl;
}
else {
   cout << userInput << endl;
}
 
It worked, thank you!
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 14 ·
Replies
14
Views
34K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K