Write a program that reads in a line consisting of a student
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
Solution
#include <iostream>
using namespace std;
int main()
{
string str;
int name, ssn, userID, password; //indexes
cout << \"Enter a student\'s name(first and last), social security number, user id, and password in one line:\" << endl;
getline(cin, str); //input in one line
name = str.find(\' \', 0); // name will store index of space from 0 index,space between first name and last name
ssn = str.find(\' \', name + 1); // ssn will store index of first space after last name
str.replace(ssn + 1, 3, \"xxx\"); // replace 3 characters with x after space
str.replace(ssn + 5, 2, \"xx\"); // replace 2 characters with x after hyphen
str.replace(ssn + 8, 4, \"xxxx\"); //replace 4 characters with x after hyphen
userID = str.find(\' \', ssn + 1); //find space index after ssn
password = str.find(\' \', userID + 1); //find space character after user is for password
str.replace(password + 1, 10, \"xxxxxxxx\"); //replace all caracters of password with x
cout << str << endl; //display the manipulated string
return 0;
}
output:
