Question

In: Computer Science

(Please write in C++) Write a program that reads in a line consisting of a student’s...

(Please write in C++) 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
strVar.at(index)
strVar[index]
strVar.append(n, ch)
strVar.append(str)
strVar.clear()
strVar.compare(str)
strVar.empty()
strVar.erase()
strVar.erase(pos, n)
strVar.find(str)
strVar.find(str, pos)
strVar.find_first_of(str, pos)
strVar.find_first_not_of(str, pos)
strVar.insert(pos, n, ch)
strVar.insert(pos, str)
strVar.length()
strVar.replace(pos, n, str)
strVar.substr(pos, len)
strVar.size()
strVar.swap(str1)

With an input of: John Doe 333224444 DoeJ 123Password

The results should be: John Doe xxxxxxxxx DoeJ xxxxxxxxxxx

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

#include <iostream>
#include<string>
using namespace std;

int main() {
string data;
   cout<<"Enter input: ";
   getline(cin,data);
  
   cout<<data<<endl;
   int ssn_start_index=0;
   int ssn_end_index=0;
   int pass_start_index=0;
   int pass_end_index=data.size()-1;
   int space_count=0;
  
   // loop through the string
   for(int i=0;i<data.size();i++)
   {
   // is the character a space?
  
   if(data.at(i)==' ')
   {
   // increase the count
   space_count++;
   // ssn starts from next character
   if(space_count==2)
   {
   ssn_start_index=i+1;
   }
   // ssn ends here
   if(space_count==3)
   {
   ssn_end_index=i-1;
   }
  
   // password starts from next character
   if(space_count==4)
   {
   pass_start_index=i+1;
   }
   }
   }
  
     
   // Now , we know the index for start and end for both password and SSN number
   // replace them with *
   for(int pos=ssn_start_index;pos<=ssn_end_index;pos++)
   data.replace(pos,1,"*");
   for(int pos=pass_start_index;pos<=pass_end_index;pos++)
   data.replace(pos,1,"*");
   cout<<data;
  
      
   return 0;
}

===========


Related Solutions

In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
In c++, write a program that reads a string consisting of a positive integer or a...
In c++, write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format.
Write a C++ program that reads a file consisting of students’ test scores in the range...
Write a C++ program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176,...
Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Use C++ to write a program that reads in a binary string from the command line...
Use C++ to write a program that reads in a binary string from the command line and applies the following (00, 1101) tag-system: if the first bit is 0, deletes the first three bits and append 00; if the first bit is 1, delete the first three bits and append 1101. Repeat as long as the string has at least 3 bits. Try to determine whether the following inputs will halt or go into an infinite loop: 10010, 100100100100100100. Use...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains an A for arrival or a D for departure, which is terminated by a :, and a license plate number, which is terminated by a :. The program should print a message each time a car arrives or departs. When a car arrives, the message should specify when the garage is full. If there is no room for a car, the car simply leaves....
C++ Write a program that reads a line of text, changes each uppercase letter to lowercase,...
C++ Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). Please use a Queue Class and Stack class.
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT