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's answer

#include <iostream>
#include <string>
#include <cctype>
#include <ctime>

using namespace std;

void Replace_symbol(char &elem)
{
	int symbol = rand() % 4+1;
	if (symbol == 1) elem = '_';
	if (symbol == 2) elem = '&';
	if (symbol == 3) elem = ';';
	if (symbol == 4) elem = '#';
}

int main() {
	string passCode;
	cin >> passCode;
	srand(time(NULL));
	if (isalpha(passCode[0]))
	{
		Replace_symbol(passCode[0]);
	}
	if (isalpha(passCode[1]))
	{
		Replace_symbol(passCode[1]);
	}
	cout << passCode << endl;
	system("pause");
	return 0;


}
LATEST TUTORIALS
APPROVED BY CLIENTS