MHB C ++ typing censored for 'darn'

  • Thread starter Thread starter adrumheiser
  • Start date Start date
AI Thread 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!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
7
Views
7K
Replies
4
Views
6K
Replies
14
Views
34K
Replies
1
Views
5K
Replies
5
Views
3K
Back
Top