Write a program that reads in a line consisting of a student’s name (first and l
ID: 3807477 • Letter: W
Question
Write a program that reads in a line consisting of a student’s name (first and last), Social Security number, user ID, and password.
Use getline not cin so you can demonstrate an understanding of the string manipulation concepts The program outputs the string in which all the digits of the Social Security number and all the characters in the password are replaced by x. The Social Security number is in the form 000-00-0000, and the user ID and the password do not contain any spaces.
Your program should not use the operator [] to access a string element.
Enter a student's name social security number user id, and password in one line: Jane Smith 222-33-4444 S12345 password Jane Smith xxx-xx-xxxx S12345 xxxxxxxx Press any key to continueExplanation / Answer
Note that as The Social Security number is in the form 000-00-0000, we can directly output XXX-XX-XXXX for it.
And for Password, we need to output X, number of times the length of password.
C++ code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
std::string line;
std::cout << "Please, enter a student's name, social security number, user-id, password in one line: ";
std::getline (std::cin,line);
string buf; // Have a buffer string
stringstream ss(line); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.push_back(buf);
cout << tokens[0] << " " << tokens[1] << " " << "XXX-XX-XXXX" << " " << tokens[3] << " ";
for (int i = 0; i < tokens[4].size(); ++i)
{
cout << "X";
}
return 0;
}
Sample Output:
Please, enter a student's name, social security number, user-id, password in one line:
Jane Smith 222-33-4444 S12345 pass
Jane Smith XXX-XX-XXXX S12345 XXXX
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.