Answer the question
In order to leave comments, you need to log in
The original array is truncated after the copy function, why?
Write a program for encoding (decoding) text according
to the “complement” principle: in an English text, the letter a changes to the letter
z, the letter b changes to the letter y, the letter c changes to the letter x, etc.
And if it's not difficult, you can tell me how to pass the revers function to the crypt function.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string.h>
#include <locale>
#include <cctype>
using namespace std;
void abc(char _abc[])
{
int i,j = 0;
for(int i = 97; i < 123;++i)
_abc[j++] = (char)i;
_abc[j] = '\0';
}
void revers(char _abc[])
{
int temp;
int i;
for(i=0;i<strlen(_abc)/2;i++)
{
temp = _abc[i];
_abc[i] = _abc[strlen(_abc)-i-1];
_abc[strlen(_abc)-i-1] = temp;
}
}
void crypt(char *msg,char *_abc,char* _abcReverse,char *cryptArray)
{
int i,j;
int index[strlen(msg)] ;
for(i=0;i<strlen(msg);i++)
{
for(j=0;j < strlen(_abc);j++)
{if (_abc[j] == msg[i]) index[i] = j;}
}
for(i=0;i<strlen(msg);i++) cout << index[i] << " "; // массив заполянется нулями
}
void out(char str[])
{
for(int i = 0; i < strlen(str);i++) cout << str[i] << " ";
}
int main()
{
char _abc[25]="";
char _abcReverse[25] = "";
char msg[] = "abc";
char *crypArray = new char[strlen(msg)];
abc(_abc);
out(_abc);
cout << endl;
strcpy(_abcReverse,_abc); // после копирования масива исходный массив обрезается и в нём остаётся один символ
revers(_abcReverse);
out(_abcReverse);
cout << endl;
crypt(msg,_abc,_abcReverse,crypArray);
cout << endl;
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
#include <iostream>
void encode(char *msg)
{
for (int i = 0; msg[i]; ++i)
{
msg[i] = 'z' - (msg[i] - 'a');
}
}
int main()
{
char msg[] = "hello";
encode(msg);
std::cout << msg << std::endl;
}
system("pause")
char _abc[25]="";
There are 26 letters in the Latin alphabet + a null character = 27. You have a classic buffer overflow. Most likely the extra letters are written into the memory of the _abcReverse array, and when _abcReverse was used for its intended purpose, the extra characters were overwritten.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question