163k views
5 votes
A website requires that passwords do not contain whitespace. For each character in the passphrase that is whitespace, replace the character with *! For example, if the input is "ir 2", then the output is:

New password: "1r**!**2"

Note: isspace returns true if a character is whitespace and false otherwise. For example, isspace(' ') returns true, and isspace('a') returns false.

#include
#include

using namespace std;

int main() {
string passphrase;

if (getline(cin, passphrase)) {
cout << "New password: ";

for (unsigned int i = 0; i < passphrase.length(); ++i) {
if (isspace(passphrase[i])) {
cout << "*!";
} else {
cout << passphrase[i];
}
}

cout << endl;
}

return 0;
}

1 Answer

0 votes

Final answer:

The question is about modifying a string in C++ to replace whitespace with '*!' using a for loop and the isspace function, demonstrating basic string manipulation in programming.

Step-by-step explanation:

The student's question pertains to a common task in programming: modifying a string according to specific rules. In this case, the requirement is to replace each whitespace character in a passphrase with the characters '*!' without using any built-in string replacement functions.

Instead, the solution provided uses a simple for loop to iterate over each character in the input string and employs the isspace function to check for whitespace characters. When a whitespace is detected, '*!' is printed instead of the space; otherwise, the original character is printed. This is a basic yet effective method for string manipulation in C++ programming.

User MarTic
by
8.3k points