Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that reads text from a file and encrypts the file by adding

ID: 3575446 • Letter: W

Question

Write a C++ program that reads text from a file and encrypts the file by adding 4 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 4 to it. 3. Write the encoded string to a second file, such as coded.txt 4. The encoded file should have the same structure as the original file, if the second line of the original file has 24 characters (including spaces) then there should be 24 characters on the second line of the encoded file (spaces will now look like '$')

plain .txt

Explanation / Answer

#include <bits/stdc++.h>

using namespace std;
int main( )
{

  

  
  
   ifstream infile("plain.txt");
   ofstream outfile ("coded.txt",std::ios::out);

   while (infile.good())
{
  
       int BUF_SIZE=100;
char line[BUF_SIZE];
memset(line,0,sizeof(line));
  
if( infile.getline(line, BUF_SIZE))
{
  
  
            for (int i = 0; i < strlen(line); ++i)

            {
                cout<<line[i];
                line[i]=line[i]+4;
            }
            outfile<<line<<endl;

}

  
  
  
}

  
}

===============================================================

akshay@akshay-Inspiron-3537:~/Chegg$ g++ encryp.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out

===============================================================

pain.txt

Your program should encode this
file by adding four to the AsCII value of each
character in it. In your encoded file the first character should
be 'X'. The last character of the first line should be ':'.
The encoded file should have 8 lines of text just like this one,
but will be unreadable. The last character of the last line will
be a '2'.

=============================================================================

coded.txt

]syv$tvskveq$wlsyph$irgshi$xlmw
jmpi$f}$ehhmrk$jsyv$xs$xli$EwGMM$zepyi$sj$iegl$
glevegxiv$mr$mx2$Mr$}syv$irgshih$jmpi$xli$jmvwx$glevegxiv$wlsyph$
fi$++2$Xli$pewx$glevegxiv$sj$xli$jmvwx$pmri$wlsyph$fi$+>+2
Xli$irgshih$jmpi$wlsyph$lezi$<$pmriw$sj$xi|x$nywx$pmoi$xlmw$sri0
fyx${mpp$fi$yrviehefpi2$Xli$pewx$glevegxiv$sj$xli$pewx$pmri${mpp$
fi$e$+6+2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote