• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[C++]Help in starting the code

Status
Not open for further replies.
Level 20
Joined
Apr 14, 2012
Messages
2,901
I want to make a program that does a basic substitution cipher based on an input integer. For example, I type first '3', then the next message I type is "ABC", then each letter is moved an n number of letters on the alphabet line. So "ABC" becomes "DEF".

What I can't figure out is, how do I initialize and set all of this up? Do i create variables for each of the letter of the alphabet and for each lower and uppercase? But also, how do I make the code skip the spaces?

I would appreciate any type of help.
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,190
In theory you can do it with an ascii chart as mentioned above. However in reality your program description is a bit rubbish as not all localities use the English alphabet.

What one should do is to specific an encoding which defines the boundaries of the cipher. One must also specify what happens to characters outside this boundary (are they deleted? or ignored? etc).
 
Level 20
Joined
Apr 14, 2012
Messages
2,901
Code:
#include <iostream>
#include <string>

	
using namespace std;

int main()
{
	string input;
	int count = 0, length;

	cout <<"Enter your phrase \n";
	getline(cin, input);

	length = (int)input.length();

	for(count = 0; count < length; count++)
	{
		if(isalpha(input[count]))
		{
			input[count] = tolower(input[count]);
			for (int i = 0; i < 13; i++)
			{
               if(input[count] == 'z')
               	  input[count] = 'a';
               	else
               		input[count]++
		}
			}
	}

	cout << "Results: \n"  << input << endl;
}

Hmm...
So I made this code based on a video I found. It sets the letters 13 spaces from where it is originally from. But this is only for encryption. How do I make the code that decrypts?

I tried transforming all the ++ symbols to --, but that just messed things up. The video also said that I could change the index from 13 to any other number, but I'd have to have two of these:
Code:
for(count = 0; count < length; count++)
	{
		if(isalpha(input[count]))
		{
			input[count] = tolower(input[count]);
			for (int i = 0; i < 13; i++)
			{
               if(input[count] == 'z')
               	  input[count] = 'a';
               	else
               		input[count]++
		}
			}
	}
And that I should change the other one's ++s to --s. But that also does not work. What is going wrong?
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
You don't need a loop.

You know that your letter space so to speak is from 'a' to 'z' (since you used the tolower function).
That means that if you are between 'a' and 'z', you don't need to do anything. If you are above 'z', and you want to cycle back, you need to reduce 'z'-'a', and if you are below 'a' and want to cycle back, you need to add 'z'-'a'.

For example, imagine your number space is 0 to 10 (remember that the char/id notation 'x' is just a number).
If you have 5, that's fine.
If you have 15, reduce 10-0, and you get 5.
If you have -5, add 10-0, and you get 5.
If you want to handle large numbers that are twice as big, or larger, than the size of your space (not that relevant to this specific case, but still), you can use the modulo operator.

After you implement this sort of cycle, it's a simple matter of adding X to every input to encode, and subtracting X from every output to decode (or vice versa).

Also use the highlight option instead of code in the future: [code=cpp][/code]
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
MasterTrainer said:
But, how xould a letter be beyond 'a' - 'z'?

Again, that notation is just a nice way to write numbers using ASCII letters.
C++:
'a' == 97
'b' == 98
...
'z' == 122
If you want to move a letter up by some N letters, you just add a number.
C++:
'a' + 3 == 'd' == 100
Basically all you need to do is the following, if, for example, your encoding process adds 5 to each character:
C++:
char encoded = C + 5; // Where C is your input character

if (encoded > 'z') {
    encoded -= 'z' - 'a'; // Cycle from z to a
} else if (encoded < 'a') {
    encoded += 'z' - 'a'; // Cycle from a to z
}
And of course, 'z' - 'a' is just the constant 25.
 
Status
Not open for further replies.
Top