
Originally Posted by
Zlatomir
My guess is that index get to the range in the first loop, and for the second loop is out of range.
Another possible mistake is that you closed one bracket too early, so check that you didn't end the function definition before you want to.
And share more info about how you can't see the pointer, it's an compile error, a run-time error, a syntax highlight error? If you don't provide information we can only guess.
Hi I don't get compiler error. I get en error at runtime and the index has been reset to zero before the last while loop.
I paste here the whole code ( it's a very basic decryption algorithm )
/* decryptMessage function */
void decryptMessage( const char *message, const char *freqLang )
{
const char *ptr = message;
char freqLangCrypt[sizeof(freqLang)];
//printf("sizeof %d", sizeof(freqLang) );
int freq[TOTAL_LETTER], pos, c, max, maxj;
pos = c = 0;
//memset((void*)freq, 0, TOTAL_LETTER);
for (int i = 0; i < TOTAL_LETTER; i++)
freq[i] = 0;
//I read the message the first time to create freq[]
int index = 0;
while( (c = message[index]) != '\0' )
{
if (isalpha(c))
{
c = toupper(c);
pos = c - 'A';
freq[pos] = freq[pos] + 1; //freq[0] = number of 'a' and 'A' in the file
}
index++;
}
//here I create freqLangCrypt[] from freq[]
//freqLangCrypt[] frequency table of the crypted message
for(int i = 0; i < TOTAL_LETTER; i++)
{
max = 0;
maxj = -1;
//I search the maximum values in freq
for(int j = 0; j < TOTAL_LETTER; j++)
{
if(freq[j] > max)
{
max = freq[j];
maxj = j;
}
}//for
if(maxj > -1)
{
freqLangCrypt[i] = maxj + 'A';
freq[maxj] = 0;
}
}//for
//read again the message to decrypt it
index = 0;
char *pdest = 0;
while( (c = ptr[index]) != '\0' )
{
if (isalpha(c))
{
c = toupper(c);
pdest = strchr(freqLangCrypt, c);
if( pdest != NULL )
{
index = (int)(pdest - freqLangCrypt);
putchar(freqLang[index]);
}
else
putchar(c);
}//if(isalpha(c))
else
putchar(c);
index++;
}//while
}//end of decryptMessage
/* decryptMessage function */
void decryptMessage( const char *message, const char *freqLang )
{
const char *ptr = message;
char freqLangCrypt[sizeof(freqLang)];
//printf("sizeof %d", sizeof(freqLang) );
int freq[TOTAL_LETTER], pos, c, max, maxj;
pos = c = 0;
//memset((void*)freq, 0, TOTAL_LETTER);
for (int i = 0; i < TOTAL_LETTER; i++)
freq[i] = 0;
//I read the message the first time to create freq[]
int index = 0;
while( (c = message[index]) != '\0' )
{
if (isalpha(c))
{
c = toupper(c);
pos = c - 'A';
freq[pos] = freq[pos] + 1; //freq[0] = number of 'a' and 'A' in the file
}
index++;
}
//here I create freqLangCrypt[] from freq[]
//freqLangCrypt[] frequency table of the crypted message
for(int i = 0; i < TOTAL_LETTER; i++)
{
max = 0;
maxj = -1;
//I search the maximum values in freq
for(int j = 0; j < TOTAL_LETTER; j++)
{
if(freq[j] > max)
{
max = freq[j];
maxj = j;
}
}//for
if(maxj > -1)
{
freqLangCrypt[i] = maxj + 'A';
freq[maxj] = 0;
}
}//for
//read again the message to decrypt it
index = 0;
char *pdest = 0;
while( (c = ptr[index]) != '\0' )
{
if (isalpha(c))
{
c = toupper(c);
pdest = strchr(freqLangCrypt, c);
if( pdest != NULL )
{
index = (int)(pdest - freqLangCrypt);
putchar(freqLang[index]);
}
else
putchar(c);
}//if(isalpha(c))
else
putchar(c);
index++;
}//while
}//end of decryptMessage
To copy to clipboard, switch view to plain text mode
and the main:
#define TOTAL_LETTER 26
void decryptMessage( char* text, const char* );
int _tmain(int argc, _TCHAR* argv[])
{
char* message = new char[1143];
strcpy_s(message,1143, "Uid nx, aex jcdjipx iu wzux zp, ta wxtpa jtdaws, ai etkx vis.\
Dcos zyexdzaxr aex Jxdw jezwipijes iu etkzyg nidx aety iyx hts\
ai ri aex ptnx aezyg. Z zyexdzaxr aeta jezwipijes udin Wtdds Htww,\
hei zp ns exdi tqactwws. Z htya ai ntfx Dcos cpxdp udxx. Z htya ai\
gzkx aexn aex udxxrin ai qeiipx. Jxijwx tdx rzuuxdxya. Jxijwx qeiipx\
rzuuxdxya qdzaxdzt. Oca zu aexdx zp t oxaaxd hts tniyg ntys\
twaxdytazkxp, Z htya ai xyqicdtgx aeta hts os ntfzyg za qinuidatowx.\
Pi aeta\'p heta Z\'kx adzxr ai ri.\
Z htya ai piwkx jdiowxnp Z nxxa zy aex rtzws wzux os cpzyg qinjcaxdp,\
pi Z yxxr ai hdzax jdigdtnp. Os cpzyg Dcos, Z htya ai qiyqxyadtax aex\
aezygp Z ri, yia aex ntgzqtw dcwxp iu aex wtygctgx, wzfx patdazyg hzae\
jcowzq kizr pinxaezyg pinxaezyg pinxaezyg ai pts, \"jdzya exwwi hidwr.\"\
Z vcpa htya ai pts, \"jdzya aezp!\" Z riy\'a htya tww aex pcddicyrzyg\
ntgzq fxshidrp. Z vcpa htya ai qiyqxyadtax iy aex atpf. Aeta\'p aex otpzq\
zrxt. Pi Z etkx adzxr ai ntfx Dcos qirx qiyqzpx tyr pcqqzyqa.\
Scfzezdi Ntapcniai. (hhh.tdaznt.qin/zyak/dcos)\0");
/* in the main */
const char* freqLang = "TEOIARNSHLMYUCWDGPFBVKJ";
decryptMessage( message, freqLang );
delete[] message;
return 0;
}
#define TOTAL_LETTER 26
void decryptMessage( char* text, const char* );
int _tmain(int argc, _TCHAR* argv[])
{
char* message = new char[1143];
strcpy_s(message,1143, "Uid nx, aex jcdjipx iu wzux zp, ta wxtpa jtdaws, ai etkx vis.\
Dcos zyexdzaxr aex Jxdw jezwipijes iu etkzyg nidx aety iyx hts\
ai ri aex ptnx aezyg. Z zyexdzaxr aeta jezwipijes udin Wtdds Htww,\
hei zp ns exdi tqactwws. Z htya ai ntfx Dcos cpxdp udxx. Z htya ai\
gzkx aexn aex udxxrin ai qeiipx. Jxijwx tdx rzuuxdxya. Jxijwx qeiipx\
rzuuxdxya qdzaxdzt. Oca zu aexdx zp t oxaaxd hts tniyg ntys\
twaxdytazkxp, Z htya ai xyqicdtgx aeta hts os ntfzyg za qinuidatowx.\
Pi aeta\'p heta Z\'kx adzxr ai ri.\
Z htya ai piwkx jdiowxnp Z nxxa zy aex rtzws wzux os cpzyg qinjcaxdp,\
pi Z yxxr ai hdzax jdigdtnp. Os cpzyg Dcos, Z htya ai qiyqxyadtax aex\
aezygp Z ri, yia aex ntgzqtw dcwxp iu aex wtygctgx, wzfx patdazyg hzae\
jcowzq kizr pinxaezyg pinxaezyg pinxaezyg ai pts, \"jdzya exwwi hidwr.\"\
Z vcpa htya ai pts, \"jdzya aezp!\" Z riy\'a htya tww aex pcddicyrzyg\
ntgzq fxshidrp. Z vcpa htya ai qiyqxyadtax iy aex atpf. Aeta\'p aex otpzq\
zrxt. Pi Z etkx adzxr ai ntfx Dcos qirx qiyqzpx tyr pcqqzyqa.\
Scfzezdi Ntapcniai. (hhh.tdaznt.qin/zyak/dcos)\0");
/* in the main */
const char* freqLang = "TEOIARNSHLMYUCWDGPFBVKJ";
decryptMessage( message, freqLang );
delete[] message;
return 0;
}
To copy to clipboard, switch view to plain text mode
Regards
Bookmarks