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:
- Convert the code to C++. Replace all printf, scanf, and fgets statements.
- Add codes to the “decrypt” method to decipher the encrypted text.
- Change the code to use pointer operations instead of array operations to encrypt and decrypt messages.
- In the main method, call the “decrypt” method to decipher the encrypted text (str).
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.
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
forinto awhileloopnow, we try to get rid of the
strlencall.strlencounts 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 just0. so similar to comparingstr[i] != 32, we can writestr[i] != 0if we want to loop as long as the character at positioniof stringstris not 0.str[i]is equivalent to the following*(str + i)it is just written differently.so we can rewrite again
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
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.
and then replace your
printfcalls bystd::coutcalls 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)