帮助!我正试图弄清楚我们教授给我们的这段代码——
#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:
}
}
我们假设执行以下操作:
将
C
代码转换为C++
.将代码添加到“解密”方法以破译加密的文本。
更改代码以使用
pointer
操作而不是array
操作来加密和解密消息。在main方法中,调用“decrypt”方法对密文(str)进行解密。
这是我设法做到的,但我现在几乎被困住了。特别是因为我没有C
语言背景。任何帮助,将不胜感激。
#include <iostream>
#include <string.h>
void encrypt(int offset, char *str)
{
std::cout << "\nUnencrypted str = \n" << str;
char *pointer = str;
while(*pointer)
{
if (*pointer !=32)
*pointer = *pointer + offset;
++pointer;
}
std::cout <<"\nEncrypted str =\n" << str << "\n\nlength = ";
}
void decrypt(int offset, char *str) {
// add your code here
}
void main(void) {
char str[1024];
std::cout << "Please enter a line of text max " << sizeof(str) << " characters\n";
if (fgets(str, sizeof(str), stdin) != NULL)
{
encrypt(5, str); // What is the value of str after calling "encrypt"?
// add your method call here:
}
}