Slater
Slater

Reputation: 21

Converting simple C encryption to C++

I have some embarrassingly elementary questions that I cannot seem to find a an answer for that I am able to process. I had incredible luck the last time I asked you folks for some help, so I am hoping for a repeat. I have no prior experience in C/C++ of any kind and this class assumes at least 1 semester, so its kicking my but a bit.

This is the code I am given by my professor:

#include <stdio.h>
#include <string.h>

void encrypt(int offset, char *str) {

int i,l;

l=strlen(str);

printf("\nUnencrypted str = \n%s\n", str);

for(i=0;i<l;i++)
    if (str[i]!=32)  
        str[i] = str[i]+ offset;

printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}

void decrypt(int offset, char *str) {

// add your code here
}

void main(void) {

char str[1024];

printf ("Please enter a line of text, max %d characters\n", sizeof(str));

if (fgets(str, sizeof(str), stdin) != NULL)
{
    encrypt(5, str);    // What is the value of str after calling "encrypt"?

    // add your method call here:
}
}

This is what I am suppose to perform:

I am not asking for answers directly to the assignment but I need help undertanding the errors I am making and what I need to be doing.

this is my attempt at the first part of the homework:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

void encrypt(int offset, char *str) {

int i,l;

l=strlen(str);

cout << "\nUnencrypted str = \n%s\n", str;

for(i=0;i<l;i++)
    if (str[i]!=32)
        str[i] = str[i]+ offset;

cout << "\nEncrypted str = \n%s \nlength = %d\n", str, l;
}

void decrypt(int offset, char *str) {

// add your code here
}

int main(void) {

char str[1024];

cout << "Please enter a line of text, max %d characters\n", sizeof(str);

if (fgets(str, sizeof(str), stdin) != NULL)
{
    encrypt(5, str);    // What is the value of str after calling "encrypt"?

    // add your method call here:
}
}

problem is this is what I am getting out and I cant figure out what needs to be done differently.

Please enter a line of text, max %d characters hellow world

Unencrypted str = %s

Encrypted str = %s length = %d

At this point ill take anything, I have already spent a couple hours googling and trying things without really getting anywhere and I know this is something that should take no longer than an hour.

Upvotes: 0

Views: 1296

Answers (2)

esskar
esskar

Reputation: 10940

looking at the "Change the code to use pointer operations instead of array operations to encrypt and decrypt messages", let's see if i can explain this one. (i will focus on the pointer part). i will rewrite the encrypt function a couple of times to hopefully make it clear

first, we convert the for into a while loop

void encrypt(int offset, char *str) 
{
    int i,l;

    l=strlen(str);

    printf("\nUnencrypted str = \n%s\n", str);

    i = 0;
    while(i < l)
    {
        if (str[i]!=32)  
            str[i] = str[i]+ offset;
        ++i;
    }

    printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}

now, we try to get rid of the strlen call. strlen counts all characters between the start of a C string and the first appereance of a null character. A null character can be written as '\0' but in C or C++ that is pretty much the same as writing just 0. so similar to comparing str[i] != 32, we can write str[i] != 0 if we want to loop as long as the character at position i of string str is not 0.

void encrypt(int offset, char *str) 
{
    int i;

    printf("\nUnencrypted str = \n%s\n", str);

    i = 0;
    while(str[i] != 0)
    {
        if (str[i]!=32)  
            str[i] = str[i]+ offset;
        ++i;
    }

    printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}

str[i] is equivalent to the following *(str + i) it is just written differently. so we can rewrite again

void encrypt(int offset, char *str) 
{
    int i;

    printf("\nUnencrypted str = \n%s\n", str);

    i = 0;
    while(*(str + i) != 0)
    {
        if (*(str + i)!=32)  
            *(str + i) = *(str + i) + offset;
        ++i;
    }

    printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}

now we already have solution only using pointers, but we can make it more pretty by understanding pointers better.

let's rewrite again using another variable

void encrypt(int offset, char *str) 
{
    int i;

    printf("\nUnencrypted str = \n%s\n", str);

    i = 0;
    char *ptr = (str + i);
    while(*ptr != 0)
    {
        if (*ptr!=32)  
            *ptr = *ptr + offset;
        ++i;
        ptr = (str + i);
    }

    printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}

so all i did here is, i substituted (s + i) by the new variable ptr. and in the final step, we remove the i, since it is just a helper variable the tells us how many places we have to jump from the start address. but since it increases by just one in every iteration, we just move the pointer directly.

void encrypt(int offset, char *str) 
{
    printf("\nUnencrypted str = \n%s\n", str);

    char *ptr = str;
    while(*ptr) // the same as *ptr != 0
    {
        if (*ptr!=32)  
            *ptr = *ptr + offset;
        ++ptr;
    }

    printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}

and then replace your printf calls by std::cout calls as suggested by Seth Carnegie.

for the decryption: just try to see what is happening in encrypt: when encrypt 'sees' a chracter that is not equal to 32, it addsoffset` to that character and assigns it back. so try to think about what you have to do, to revert this operation (which has nothing to do with C at all)

Upvotes: 1

slugonamission
slugonamission

Reputation: 9642

std::iostream doesn't accept printf style formatting. Instead, just break the string and chain another << on with the argument, like

int myInt = 5;
std::cout << "HelloWorld" << myInt << '\n';

This works with all of the basic types, although for custom classes, you will need to manually add the ostream << operator.

Upvotes: 3

Related Questions