encrypt and decrypt

I wrote a program like my teacher asked to encrypt a message... I only used lower case... I have to also use upper case letters.... and then add a decryption part to the program...any suggestions will be appreciated!!

#include <iostream>

using namespace std;
void encrypt (char []);

int main()
{
char msg []= "hello z";
encrypt (msg);
cout << msg << endl;
}

void encrypt (char msg []){
int key = 15;
int i = 0;
while (msg [i] != '\0'){
if (msg[i] <= 'z' && msg[i] >= 'a'){
if (msg[i] + key <= 'z'){
msg[i] = (char)(msg[i] + key);
}
else {
int d = msg [i] + key - 'z';
msg [i]= (char)'a' + d - 1;
}
}
i ++;
}
}

Please, help me add the upper cases into my encryption and then a decryption
Not sure what the issue is, upper and lower case letters have different ASCII values.
I know I need to add some sort of loop to use upper case letters... and then i dont know how to decrypt it.. .
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 <ctype.h>

int main(){
    
    char mychars[10] = {'g','h','i','a','b','c','d','e','f', 'l'};
    
    for(int i = 0; i < 10; i++) mychars[i] = toupper(mychars[i]);//sets to upper case
    
    for(int i = 0; i < 10; i++) std::cout << mychars[i];
    std::cout << '\n'; 
    for(int i = 0; i < 10; i++) mychars[i] += 3; //encripts
    
    for(int i = 0; i < 10; i++) std::cout << mychars[i]; 
    std::cout << '\n'; 
    for(int i = 0; i < 10; i++) mychars[i] -= 3; // decripts
    
    for(int i = 0; i < 10; i++) std::cout << mychars[i]; 
    
    return 0; 
} 
} 


GHIABCDEFL
JKLDEFGHIO
GHIABCDEFL
Last edited on
Topic archived. No new replies allowed.