C++ Assignment: Write a program that reads in a line consisting of a student’s n
ID: 3938361 • Letter: C
Question
C++ Assignment:
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.
Enter a student's name, social security number, user id, and password in one line: Jane Smith 222-33-4444 512345 password Jane Smith xxx-xx-xxxx S12345 xxxxxxxx Press any key to continue .Explanation / Answer
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int pos=0,i=0,pos1=0;
char x;
string str,str1="",tempstr="";
cout<<"Enter a student's name, social security number, user id and password in one line : ";
getline(cin,str);
while(pos != -1)
{
pos = str.find(" ",i);
tempstr = str.substr(i,(pos-i));
pos1=tempstr.find("-");
if(pos1 > 0)
tempstr = "xxx-xx-xxxx";
if (pos == -1)
{
string tempstr1 = "";
for(int j=0; j<tempstr.length(); j++)
tempstr1 += "x";
tempstr = tempstr1;
}
str1 += tempstr + " ";
i += ((pos+1)-i);
}
cout<<str1;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.