85.7k views
1 vote
C++:

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().
fix :
#include
#include
#include
using namespace std;
int main() {
string passCode;
cin >> passCode;

/* Your solution goes here */

cout << passCode << endl;
return 0;
}

2 Answers

4 votes

Answer:

// Program is written in C++ Programming Language

/) Comments are used for explanatory purpose

#include <iostream>

#include <string>

#include <cctype>

#include <ctime>

using namespace std;

int main() {

// Declare and accept input for string variable passCode

string passCode;

cin >> passCode;

// declaring character array

char ch[2];

// copying the contents of the string to char array

strcpy(ch, passCode.c_str());

// Iterate char arrray ch

for(int i=0; i<2;i++)

{

// Check if first and second element is an alphabet

if((ch[i]>='a' && ch[i]<='z') || (ch[i]>='A' && ch[i]<='Z'))

{

// Replace if the condition above is true

ch[i] = '_';

}

}

// Print passCode

for (int i = 0; i < n; i++)

cout << ch[i];

return 0;

}

Step-by-step explanation:

User David Hogue
by
3.3k points
3 votes

Answer:

// Program is written in C++ Programming Language

/) Comments are used for explanatory purpose

#include <iostream>

#include <string>

#include <cctype>

#include <ctime>

using namespace std;

int main() {

// Declare and accept input for string variable passCode

string passCode;

cin >> passCode;

// declaring character array

char ch[2];

// copying the contents of the string to char array

strcpy(ch, passCode.c_str());

// Iterate char arrray ch

for(int i=0; i<2;i++)

{

// Check if first and second element is an alphabet

if((ch[i]>='a' && ch[i]<='z') || (ch[i]>='A' && ch[i]<='Z'))

{

// Replace if the condition above is true

ch[i] = '_';

}

}

// Print passCode

for (int i = 0; i < n; i++)

cout << ch[i];

return 0;

}

Explanation:

User Nikel
by
3.6k points