Instructions Write a program that reads in a line consisting of a student’s name
ID: 3726405 • Letter: I
Question
Instructions
Write a program that reads in a line consisting of a student’s name, Social Security number, user ID, and password. 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. Use the appropriate functions described in Table 7-1 below.
Expression Effect strVar.at(index) Returns the element at the position specified by index strVar[index] Returns the element at the position specified by index strVar.append(n, ch) Appends n copies of ch to strVar, where ch is a charvariable or a char constant strVar.append(str) Appends str to strVar strVar.clear() Deletes all the characters in strVar strVar.compare(str) Returns 1 if strVar > str returns 0 if strVar == str; returns 1 if strVar < str strVar.empty() Returns true if strVar is empty; otherwise it returns false strVar.erase() Deletes all the characters in strVar strVar.erase(pos, n) Deletes n characters from strVar starting at position pos strVar.find(str) Returns the index of the first occurrence of str in strVar. If str is not found, the special value string::npos is returned strVar.find(str, pos) Returns the index of the first occurrence at or after poswhere str is found in strVar strVar.find_first_of(str, pos) Returns the index of the first occurrence of any character of strVar in str. The search starts at pos strVar.find_first_not_of(str, pos) Returns the index of the first occurrence of any character of str not in strVar. The search starts at pos strVar.insert(pos, n, ch) Inserts n occurrences of the character ch at index posinto strVar; pos and n are of type string::size_type; and ch is a character strVar.insert(pos, str) Inserts all the characters of str at index pos into strVar strVar.length() Returns a value of type string::size_type giving the number of characters in strVar strVar.replace(pos, n, str) Starting at index pos, replaces the next n characters of strVar with all the characters of str. If n > length of strVar, then all the characters until the end of strVar are replaced strVar.substr(pos, len) Returns a string which is a substring of strVar starting at pos. The length of the substring is at most lencharacters. If len is too large, it means “to the end“ of the string in *strVar strVar.size() Returns a value of type string::size_type giving the number of characters in strVar strVar.swap(str1) Swaps the contents of strVar and str1. str1 is a stringvariableExplanation / Answer
Program plan:
1) Declare the required header files
2) declare the function prototypes and the name them as ncodeSS() and encodePassword()
3) Desine the main function
4) declare the requird variables
5) Prompt the user for the student name , social security number , user id, and password in a particular format
6) Read the line
7) By using the predefined functions find(), substr(), and erase () get the values into respective variables
8) Display the variables values of which the digits of social security numbers and characters of password are msked with charcter "x" , by calling the functions encodeSS() and encodePassword().
9)Define the functions encodeSS() and encodPassword().
PROGRAM CODE:
*Input : Student Name , Social Security Number, User_id*
*and password as string in a single line as*
*prompted*
*Output : Student Nmae, ss number in the form of*
* xxx-xxx-xxx, user id , and password in the form*
* of x- characters*
// include header files
#include
#include
#include
using namespace std;
Declare the function prototypes encodeSS() and encodePassword() to replace strings. Function prototypes will be declared at above the main function.
// function prototypes
string encodeSS(string);
string encodePassword(string);
//main function
int main()
{
// declare the required variables
string studentName;
string social securityNumber;
string userID;
string password;
string line; int pos=0;
Read studentname , social security number, user id and password
//prompt the user to enter the name , security
// number, ID, and password
cout<<"Enter " << end|;
cout<< "Student_Name | SS_NUMBER(000-00-0000)|"
<<"User_ID | Password"<<end|;
getline(cin, line);
//store the details into respective variable by
// knowing the position of the delimiter in the line
//get the first position of "l"
pos = line.find_first_of("l",0);
//get the student name by using substr() function
studentName = line.substr(0,pos);
//erase the substring that is obtained from the string
//line
line.erase(0,pos+2);
//get the first position of "l"
pos=line.find_first6_of("l",0);
//get the social security number by using substr()
//function
//erase th substring that is obtained from the string
//line
line.erase(0,pos+2);
//get the first position of"l"
pos=line.find_first_of ("l",0);
//get the user id by using substr() function
userID= line.substr(0,pos);
//erase the substring that is obtaine from the string
//line
line.erase(0.pos+2);
//get the first position of"l"
pos= line.find_first_of("l",0);
//get the password by using substr() function
password= line.substr(0,pos);
//erase the substring that is obtained from the string
//line
line.erase(0, line.length());
//dispaly the details with masking of the security
//number and password with charcter 'x' by calling
//the function encodeSS() and encodepassword()
cout<<" Student_Name SS_Number User_ID
<<SETW(15) << "pASSWORD" << END |;
Cout<< StudentName << ;
encodeSS(socialSecurityNumber)<<" ;
<< userID << setw(15)
<<encodePassword(password);
cout<< end|;
system ("pause");
return 0;
}
The function encodeSS() replaces the social security number digitd to xxx-xx-xxx form
//function encodeSS() that takes a string and replaces the
//Social security number with character 'x' and returns the
//modified social security number
string encodeSS(string number)
{
Replace first three letters with x-letter
for (int index =0 ; index <3; index++)
ssnumber.at(index)='x';
Replace first two letters with x-letter
for (int index =4 ; index <6; index++)
ssnumber.at(index)='x';
Replace first three letters with x-letter
for (int index =0 ; index < ssnumber.length() ; index++)
ssnumber.at(index)='x';
return ssnmber;
The function encodePassword() replaces all the password charcters with character 'x';
// function encodePassword() that takes a string and
//replaces the password with the character 'x'
and returns
// the modified password
string encodePassword(string password )
{
for (int index=0; index< passwordlength(); index++)
password.replace(index, 1,'x');
return password;
}
OUTPUT
Enter
Student_Name | SS_ Number(000-00-0000)| User_ID | Password
Mark Williams | 123-67-1234 | MarkWili | 123abcde
Student_Name SS_ Number User_ID Password
Mark Williams xxx-xx-xxxxx MarkWili xxxxxx
press any key to continue
<<