student student Interview Question for Students Students


Country: GUYANA




Comment hidden because of low score. Click to expand.
0
of 2 vote

I am not going to draw a flow chart, but will include the working program. In essence it does the following:

1. Gets key
2. Opens input and output files
3. Reads every line from input file
4. XOR's every character from the read line and puts into a temporary buffer
5. writes the buffer to the output file

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

int main(int argc, char* argv[])
{
    try
    {
        if (argc < 4)
        {
            cout << argv[0] << "\n";
            cout << "Usage: cipher key infile outfile\n";
        }
        else
        {
            string line;
            stringstream ss;
            ss << argv[1];
            unsigned int key = 0;
            ss >> key;
            cout << "Key is " << key << "\n";
            if (key > 255)
            {
                cerr << "Key value is to high 255 < " << key << "\n";
                return 1;
            }
            ifstream infile(argv[2]);
            ofstream outfile(argv[3]);
            if (infile.is_open())
            {
                while (infile.good())
                {
                    getline(infile, line);
                    int lineLen = line.length();
                    if (lineLen > 4096)
                    {
                        cerr << "String in file is too long 4096 < " << lineLen << "\n";
                        infile.close();
                        outfile.close();
                        return 1;
                    }

                    char toCipher[lineLen];
                    for (int ii = 0; ii < lineLen; ii++)
                    {
                        toCipher[ii] = line[ii] ^ key;
                    }
                    outfile << toCipher;
                }
                infile.close();
                outfile.close();
            }
        }
        return 0;
    }
    catch(...)
    {
        cerr << "An unknown error occured." << endl;
        return 1;
    }

}

- nindza November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. this is C++ and not C
2. no abstraction at all, there should be a function that performs the encryption/decryption AND a test-program to get these from the user and pass on to the function.
3. Buffered, binary read should be used, not lines - it is ways more effective

- Selmeczy, P├ęter November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isn't this wrong?
I mean we will have to write letter by letter to the buffer because we are incrementing i by 1 in the for loop and we are XORing the input string characterwise.

- RShah February 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can store the placeholder in the file to indicate that file is encrypted and hash of the key also, hash of the key can be used during decryption.

- Dhiraj November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hey can we please get this in c language?

- diva November 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi there :) i tried to run that program in Dev++ plus it was jus cutting off it was not running! could anyone please help me out with why is that so? you see i am really new to c programming so i need lots of help.

thank you

- diana November 17, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More