vendredi 10 septembre 2021

How do i fix error at f letter and so on at rot13 c program

i'm new at programming. i'm currently making a rot13 program in c, i tried using "for","if" and it works untill letter f it shows question mark. if i were to write letters from a-z the encrypted results is like : nopqrstuvwxyzabcde???????? . i tried to change the addition/substraction values, the variable, the int. it didn't came out well.

#include<stdio.h>
 
int main()
{
    char message[100], ch;
    int i;
    printf("Enter a message to encrypt: ");
    gets(message);
    for(i = 0; message[i] != '\0'; ++i){
        ch = message[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = ch + 13;
            if(ch > 'z'){
                ch = ch - 26;
            }
            message[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            ch = ch + 13;
            if(ch > 'Z'){
                ch = ch - 26;
            }
            message[i] = ch;
        }
    }
    printf("Encrypted message: %s", message);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire