Difficult exercise

Hey guys! Could you give me some tips on this reddit 3 easy challenge.

Welcome to cipher day!
write a program that can encrypt texts with an alphabetical caesar cipher. This cipher can ignore numbers, symbols, and whitespace.
for extra credit, add a "decrypt" function to your program!

I do realise that i need to use getline but further from that im pretty stuck. Im weak using strings and chars.
Hi,

Caesar cipher are pretty easy and simple, just take the rotation value from user and add that to your char

post your code and tell us what problem you are facing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int main()
{

    string longone;

        getline(cin,longone);
        cout << longone << endl;
         cout << longone.length() << endl;

          for(int i = 0; i < longone.length(); i++)
          {
              cout << longone[i] << ;
          }


}


So basically my problem is that i know that I should somehow make a become the a + x character.
So if the key to how much we want to pivot(if thats a correct word) is 3: a would become d
But i do not know how to do it. Just a tip would be helpful. :)
tip:

1
2
3
4
5
6
7
char a='A';
cout<<"before: "<<a;

a+=3;// <--this is important step

cout<<"Pivot :3\nafter"
cout<<a;
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
#include <iostream>
#include <string>

using namespace std;

int main()
{

 // cout << "ivesk" << endl;
  string ilgas;
   // cin >> ilgas;
 char a;
  int key;
   cout << "Enter the number you want to pivot this thing " << endl;
    cin >> key;
             for(char i = 'a'; i <= 'z'; i++)
             {

                 cout << i;

            }
              cout << endl;
              for(char i = 'a'; i <= 'z'; i++)
              {
                  a = i;
                  a = a+key;
                  while(a > 'z')
                   {
                       a = 'a';
                       ++a;
                   }



                   cout << a;
              }
}


I'm not sure how should i make char a become 'a' again once it reaches letter 'z'.

EDIT: And go with that letter further. What I mean is begin this cycle once again
Last edited on
1
2
3
a = (((a - 97) + key) % 26) + 97;//for small case alphabets

a = (((a - 65) + key) % 26) + 65;//for upper case alphabets 

Here you go

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
#include <iostream>
#include <string>

using namespace std;

int main()
{

 // cout << "ivesk" << endl;
  string ilgas;
   // cin >> ilgas;
 char a='a';
  int key;
   cout << "Enter the number you want to pivot this thing " << endl;
    cin >> key;
             for(char i = 'a'; i <= 'z'; i++)
             {

                 cout << i;

            }
              cout << endl;
              for(char i = 'a'; i <= 'z'; i++)
              {a=i;
                 a = (((a - 97) + key) % 26) + 97;//for small case alphabets



                   cout << a;
                   
              }
}

Enter the number you want to pivot this thing 
3
abcdefghijklmnopqrstuvwxyz
defghijklmnopqrstuvwxyzabc 


EDIT: there was a post between my two post, and somehow it is deleted 0_O
Last edited on
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
#include <iostream>
#include <string>

using namespace std;

int main()
{

 cout << "enter word" << endl;
  string ilgas;
   cin >> ilgas;
cout << "enter integer" << endl;
 int key;
  cin >> key;
   char a;
  for(int i = 0; i < ilgas.length(); i++)
  {

            a = (((a - 97) + key) % 26) + 97;
             ilgas[i] = a;
              cout << ilgas[i];


}
}


I think it does work properly, right?
Last edited on
I posted that I did give up, but then I did get my shit together and finally made that program to encrypt a certain word with only lowercase letters. The code above is wrong and I now know why is it. :)

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
#include <iostream>
#include <string>

using namespace std;

int main()
{

 cout << "enter word" << endl;
  string ilgas;
   cin >> ilgas;
cout << "enter integer" << endl;
 int key;
  cin >> key;
   char a;
   char b;
  for(int i = 0; i < ilgas.length(); i++)
  {

            ilgas[i] = (((ilgas[i] - 97) + key) % 26) + 97;

              cout << ilgas[i];





 }

}


EDIT: Is it possible in the way i written this program to be able to make a decryption function on a side?
Last edited on
glad I could help
Topic archived. No new replies allowed.