Ceasar cipher

As you can see in the code the first encrypter moves the letters 13 steps for the five first letters and the second moves the letters 7 steps for the next five letters. I would like to keep on going with that so letter 11-15 moves 13 steps, letter 16-20 moves 7 steps etc, without creating a endless code. How could I do that? Should I use a loop of some sort?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>

using namespace std;
int main()
{
    string input;
    int cnt=0, length;
    
    cout << "Skriv nĂ¥got: ";
    getline(cin, input);
    string str = input.substr (0,5);
    
    length = (int)str.length();
    
    for( cnt=0; cnt < length; cnt++)
    {
        if(isalpha(str[cnt]))
        {
            str[cnt] = tolower(str[cnt]);
            for (int i=0; i< 13; i++)
            {
                if(str[cnt] == 'z')
                    str[cnt] = 'a';
                else
                    str[cnt]++;
            }
        }
            
    }
    
    string str2 =input.substr(5,5);
    
    length = (int)str2.length();
    
    for( cnt=0; cnt < length; cnt++)
    {
        if(isalpha(str2[cnt]))
        {
            str2[cnt] = tolower(str2[cnt]);
            for (int i=0; i< 7; i++)
            {
                if(str2[cnt] == 'z')
                    str2[cnt] = 'a';
                else
                    str2[cnt]++;
            }
        }
        
    }
    
    
    cout << "Resultat: " << str << str2 << endl;
    
    return 0;
}
If I were you, the first thing I'd do is separate my character encryption logic into a separate function so that I don't have to think about it while trying to figure out how to switch the rotation every 5 characters. Have a function that takes a character and a rotation, then returns an encrypted character. Then, you can loop over the entire string and every 5 steps change the rotation parameter that's passed to the encrypt function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <cctype>

// Takes the character argument ptChar (plaintext character) and rotates it
// by whatever is specified in rotAmount if ptChar is a letter of the alphabet
// If ptChar is not a letter of the alphabet, it returns ptChar as is.
char rotCipherEncrypt(char ptChar, unsigned short rotAmount)
{
    if(std::isalpha(ptChar))
    {
        ptChar = tolower(ptChar);
        for (int i = 0; i < rotAmount; i++)
        {
            if(ptChar == 'z')
                ptChar = 'a';
            else
                ptChar++;
        }
    }
    return ptChar;
}

// program entry point
int main()
{
    std::string plaintext;
    std::cout << "Enter plaintext: ";
    std::getline(std::cin, plaintext);

    std::string cipherText = plaintext;
    unsigned short rotation = 13;
    for(int i = 0; i < plaintext.size(); i++)
    {
        cipherText[i] = rotCipherEncrypt(plaintext[i], rotation);

        // if the next character falls on the other side of a 5-char boundary
        // we need to change the rotation we're using
        if( (i + 1) % 5 == 0)
        {
            if(rotation == 13) rotation = 7;
            else rotation = 13;
        }
    }

    std::cout << plaintext << "\nencrypts to\n" << cipherText << std::endl;
    return 0;
}
What if I want to use my code and just make some changes to it instead of writing a hole new.
Write code that will encrypt the whole string by moving the letter 13 steps. Don't worry about the 7 step part yet. Post that code here.
Like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

using namespace std;
int main()
{
    string input;
    int cnt=0, length;
    
    cout << "Write something: ";
    getline(cin, input);
    
    length = (int)input.length();
    
    for( cnt=0; cnt < length; cnt++)
    {
        if(isalpha(input[cnt]))
        {
            input[cnt] = tolower(input[cnt]);
            for (int i=0; i< 13; i++)
            {
                if(input[cnt] == 'z')
                    input[cnt] = 'a';
                else
                    input[cnt]++;
            }
        }
            
    }
    
    cout << "Result: " << input << endl;
    
    return 0;
}
You have the code to rotate a character an arbitrary amount of times.
1
2
3
4
5
6
7
8
        input[cnt] = tolower(input[cnt]);
        for (int i=0; i< 13; i++)
        {
            if(input[cnt] == 'z')
                input[cnt] = 'a';
            else
                input[cnt]++;
        }

13 can be replaced by a variable, to indicate how many rotations can be done and input[cnt] can be replaced by the current character to rotate.
Like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void rotate_char( char& c, int amount ) 
{
    if( isalpha( input[cnt] ) ) {
        c = tolower( input[cnt] );
        for( int i = 0; i < amount; i++ ) {
            if( c == 'z' ) c = 'a';
            else c++;
        }
    }
    else {
        cout << "invalid character!\n";
        return;
    }
}
Topic archived. No new replies allowed.