# Substitution Cipher Program Problem...

• 12-22-2005
Junior89
Substitution Cipher Program Problem...
Hi guys. I was bored today and wanted to make a program that did substitution ciphers. I made it and am now testing the actual Algorithm for it but it is not working. It will replace the first letter its told to but if you are substituting 2 letters for 2 other letters or even the whole alphabet for something else for a true substitution cipher it will not work. I know i should be using Strings instead of Char Arrays and that some of the Variable names arent great just remember i was writing this up quick and i am not familiar with strings enough to implement them in this situation. Thanks for any and all help you can give. :D

Code:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
//single-letter substitutions cipher program
//infinite loop
while(1)
{
//clear screen
system("cls");
//title
cout<<"Mono-Alphabetic Substitution Cipher\n";
cout<<"===================================\n\n";
cout<<"(1) Substitute\n";
cout<<"(2) Exit\n\n";
//choice
int mchoice;
cout<<"Choice: ";
cin>>mchoice;
cin.ignore();
//evaluate
if(mchoice==1)
{
//encrypt
char ptext[50];
char lettersto[30];
char replacements[30];
cout<<"Text to encrypt: ";
cin.getline(ptext, 50);
cout<<"Letters to replace in text:      ";
cin.getline(lettersto, 26);
cout<<"Replace with what (respectively): ";
cin.getline(replacements, 26);
//begin replacement
int i=0;
int j=0;
int k;
int n;
int L=(strlen(ptext));
int M=(strlen(lettersto));
//check some stuff
if(M != (strlen(replacements)))
{
cout<<"Replacement Mismatch\n";
cin.ignore();
break;
}
//for loops
//ENCRYPTION------------------------------
for(i; i<M; i++)
{
k = int(lettersto[i]);
for(j; j<L; j++)
{
n = int(ptext[j]);
if(k == n)
{
//replace letter
ptext[j] = replacements[i];
}
}
}
//ENCRYPTION-----------------------------

cout<<ptext;
cout<<"\n\n";
cin.ignore();
continue;
}
if(mchoice==2)
{
//exit
cout<<"Exitting...";
Sleep(1500);
break;
}
else{
cout<<"\nIncorrect Choice!!!\n";
cin.ignore();
}
}
return 0;
}

• 12-22-2005
Daved
Your for loops don't initialize the counter variable to 0. It looks like you try to do this elsewhere, but it causes a problem in the nested loop because the second time through j is not reset to 0.
• 12-22-2005
Junior89
Once again, many thanks for all of your help Daved, i really appreciate it! :D
• 12-22-2005
dwks
You'll need <cstring> for strlen(), and <sstdlib> for system.
• 12-23-2005
Salem
Writing a function called say substitute() would help simplify main()
• 12-23-2005
dwks
The initialization part of a for loop can be empty (actually, all parts can):
Code:

for(i; i<M; i++)
->
Code:

for( ; i<M; i++)
• 12-23-2005
treenef
• 12-24-2005
dwks
Oh, and strlen() returns type size_t, not int. :)
• 12-25-2005
Junior89
Thanks guys for all of the suggestions, the program is running fine. Like i said earlier though it was just for fun :) Thanks again everyone!

Perhaps someone could help me in my attempt to learn strings and show me how to make this program using strings? Thanks!
• 12-27-2005
dwks
Maybe you should post your C-string code first.
• 12-28-2005
Daved
>> Perhaps someone could help me in my attempt to learn strings and show me how to make this program using strings?

Convert
Code:

char ptext[50];
to
Code:

string ptext;
Do the same with the other string variables.

Convert
Code:

cin.getline(ptext, 50);
to
Code:

getline(cin, ptext);
Do the same for the other calls to getline.

Convert
Code:

strlen(ptext)
to
Code:

ptext.length()
or
Code:

ptext.size()
Do the same for all calls to strlen.

Your original program should have had #include <cstring> because you used strlen. The new code with C++ strings won't use strlen, so you won't need <cstring>. Your original code didn't use the C++ string class, so it didn't need #include <string>. The new code will use the C++ string class, so you should keep that in there.

You might be able to take advantage of built-in replace functionality to make your encryption code simpler, but it should still work the way it is with the C++ string class.
• 12-28-2005
dwks
Oh yes, and you don't need your continue statement.
• 12-28-2005
Daved
>> Oh yes, and you don't need your continue statement.

This is incorrect. If you take it out the program won't work properly.
• 12-28-2005
dwks
Yes, you're right. You'd also need to make the if(mchoice==2) into an else if. Never mind then.