[solved] – Question 99430
Write an expression to detect that the first character of userInput matches firstLetter.
#include <iostream>
#include <string>
using namespace std;
int main() {
string userInput;
char firstLetter;
getline(cin, userInput);
cin >> firstLetter;
if (/* Your solution goes here */) {
cout << “Found match: ” << firstLetter << endl;
}
else {
cout << “No match: ” << firstLetter << endl;
}
return 0;
}
Expert Answer
[solved] – Question 99431
Set hasDigit to true if the 3-character passCode contains a digit.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
bool hasDigit;
string passCode;
hasDigit = false;
cin >> passCode;
/* Your solution goes here */
if (hasDigit) {
cout << “Has a digit.” << endl;
}
else {
cout << “Has no digit.” << endl;
}
return 0;
}
Expert Answer
[solved] – Question 99433
Replace any alphabetic character with ‘_’ in 2-character string passCode. Ex: If passCode is “9a”, output is:
9_
Hint: Use two if statements to check each of the two characters in the string, using isalpha().
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string passCode;
cin >> passCode;
/* Your solution goes here */
cout << passCode << endl;
return 0;
}
Expert Answer
[solved] – Question 99434
Assign secretID with firstName, a space, and lastName. Ex: If firstName is Barry and lastName is Allen, then output is:
Barry Allen
#include <iostream>
#include <string>
using namespace std;
int main() {
string secretID;
string firstName;
string lastName;
cin >> firstName;
cin >> lastName;
/* Your solution goes here */
cout << secretID << endl;
return 0;
}
Expert Answer
[solved] – Question 99435
Modify songVerse to play “The Name Game” (see OxfordDictionaries.com), by replacing “(Name)” with userName but without the first letter.
Ex: If userName = “Katie” and songVerse = “Banana-fana fo-f(Name)!”, the program prints:
Banana-fana fo-fatie!
Ex: If userName = “Katie” and songVerse = “Fee fi mo-m(Name)”, the program prints:
Fee fi mo-matie
Note: You may assume songVerse will always contain the substring “(Name)”.
#include <iostream>
#include <string>
using namespace std;
int main() {
string userName;
string songVerse;
getline(cin, userName);
userName = userName.substr(1, userName.size() – 1); // Remove first character
getline(cin, songVerse);
// Modify songVerse to replace (Name) with userName without first character
/* Your solution goes here */
cout << songVerse << endl;
return 0;
}
Expert Answer
[solved] – Question 99436
Print “Censored” if userInput contains the word “darn”, else print userInput. End with newline. Ex: If userInput is “That darn cat.”, then output is:
Censored
Ex: If userInput is “Dang, that was scary!”, then output is:
Dang, that was scary!
Note: If the submitted code has an out-of-range access, the system will stop running the code after a few seconds, and report “Program end never reached.” The system doesn’t print the test case that caused the reported message.
#include <iostream>
#include <string>
using namespace std;
int main() {
string userInput;
getline(cin, userInput);
/* Your solution goes here */
return 0;
}
Expert Answer
[solved] – Question 99438
Create a conditional expression that evaluates to string “negative” if userVal is less than 0, and “non-negative” otherwise. Ex: If userVal is -9, output is:
-9 is negative.
#include <iostream>
#include <string>
using namespace std;
int main() {
string condStr;
int userVal;
cin >> userVal;
condStr = /* Your solution goes here */;
cout << userVal << ” is ” << condStr << “.” << endl;
return 0;
}
Expert Answer
[solved] – Question 99440
Using a conditional expression, write a statement that increments numUsers if updateDirection is 1, otherwise decrements numUsers. Ex: if numUsers is 8 and updateDirection is 1, numUsers becomes 9; if updateDirection is 0, numUsers becomes 7.
Hint: Start with “numUsers = …”.
#include <iostream>
using namespace std;
int main() {
int numUsers;
int updateDirection;
cin >> numUsers;
cin >> updateDirection;
/* Your solution goes here */
cout << “New value is: ” << numUsers << endl;
return 0;
}
Expert Answer
[solved] – Question 99442
Write an expression that will cause the following code to print “Equal” if the value of sensorReading is “close enough” to targetValue. Otherwise, print “Not equal”. Ex: If targetValue is 0.3333 and sensorReading is (1.0/3.0), output is:
Equal
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double targetValue;
double sensorReading;
cin >> targetValue;
cin >> sensorReading;
if (/* Your solution goes here */) {
cout << “Equal” << endl;
}
else {
cout << “Not equal” << endl;
}
return 0;
}
Expert Answer
[solved] – Question 99450
Write the definition of a class Counter containing:
An instance variable named counter of type int.
A constructor that takes one int argument and assigns its value to counter
A method named increment that adds one to counter. It does not take parameters or return a value.
A method named decrement that subtracts one from counter. It also does not take parameters or return a value.
A method named get_value that returns the value of the instance variable counter.
Expert Answer