Caesar Cipher

I am making a simple caesar cypher with a 2D array

I have to make a function for the key shift so

key=5
ABCDEFGHIJKLMNOPQRSTUVWXYZ
FGHJKL...ABCDE

Function Name: Key_Array
This function uses the random key value to make a key array
Passed: value = random key value
Returns: alphabet = rearranged alphabet
*******************************************************************/
char Key_Array(int key)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i=0;i<26;i++)
{

if (alphabet[i] >= 'A' && alphabet[i] <= 'Z')
{
alphabet[i] = (((alphabet[i] + key - 'A' + 25) % 26) + 'A');
return alphabet[i];
}
}
}



I am having trouble outputting the rearranged alphabet to the screen also.

Yes this is a homework project. So answer as soon as possible.
Please and Thank you.
This function returns single character. Did you mean to output each character instead of returning it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Key_Array(int key)
{
    string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (int i=0;i<26;i++)
    {
        if (alphabet[i] >= 'A' && alphabet[i] <= 'Z')
        {
            alphabet[i] = (((alphabet[i] + key 
                - 'A' + 25) % 26) + 'A');
            cout << alphabet[i];
        }
    }
    cout << endl;
}

int main()
{
    //call like this once:
    Key_Array(5);
}
EFGHIJKLMNOPQRSTUVWXYZABCD

Last edited on
Topic archived. No new replies allowed.