0

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;
}
kaylum
  • 13,833
  • 2
  • 22
  • 31
Naher
  • 1
  • 1
    First of all, never ***ever*** use `gets`, [it's so dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) it has even been removed from C altogether. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. Secondly, please indent your code to make it easier to read and follow along in what's happening. And remember that strings in C are really called *null-terminated* strings. – Some programmer dude Sep 10 '21 at 06:28
  • 1
    The for your problem: Take this as an opportunity to learn how to use a *debugger* to step through your code statement by statement while monitoring variables and their values. – Some programmer dude Sep 10 '21 at 06:29
  • 1
    Use the ModuloOperator. sth like (letter+13)%26 so 33 becomes 7 without the if and else – Strella Sep 10 '21 at 06:29
  • alright thank you guys, much appreciated. – Naher Sep 10 '21 at 06:40

2 Answers2

1

Your code is almost okay. But there is a catch in ASCII. As we know Ascii contains both ASCII (0-127) characters and extended ASCII (128-255) characters. Ascii uses 7 bit for representation whereas extended ASCII uses 8 bit. So, when the value of ch + 13 exceeds 127 and you are subtracting 26 from it, it's basically producing a negative decimal number. You can check by print like this printf("%d", ch);. And there is no ascii character associated with negative decimal numbers. That's why it's producing garbage values. You should check before assigning ch = ch + 13. Check the below code for reference.

#include<stdio.h>

int main()
{
    char message[100], ch;
    int i;
    printf("Enter a message to encrypt: ");
    scanf("%s", message);
    for(i = 0; message[i] != '\0'; ++i){
        ch = message[i];
        if(ch >= 'a' && ch <= 'z'){
            if (ch + 13 > 'z') {
                ch = ch - 13;
            }
            else ch = ch + 13;
        
            message[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            if (ch + 13 > 'Z') {
                ch = ch - 13;
            }
            else ch = ch + 13;
            message[i] = ch;
        }
    }
    printf("Encrypted message: %s", message);
    return 0;
}
  • 1
    Actually, "ASCII" itself is an anachronism. Use "ASCII-7" when you are indeed referring to the 0..127 character range, and use [ISO/IEC 8859](https://en.wikipedia.org/wiki/ISO/IEC_8859) or [Windows code page](https://en.wikipedia.org/wiki/Windows_code_page#Windows-125x_series) terms for anything beyond that, because there is no "extended ASCII" standard. I have a nice [side-by-side chart](https://encoding.rootdirectory.de) to show the differences and similarities. – DevSolar Sep 10 '21 at 07:09
  • thank you kind sir. i need to do more research about this ascii, binary stuff and how it relates. – Naher Sep 10 '21 at 07:55
0

Most likely char is signed on your machine. So ch = ch + 13 end up being negative in some cases (e.g. when ch is z). Consequently if(ch > 'z'){ doesn't work as you expect.

Try

int main(void) {
    char ch = 'z';
    ch = ch + 13;
    if (ch > 'z')
    {
        puts("Larger");
    }
    else
    {
        puts("Less or equal");
    }
    printf("ch value is %d\n", ch);
    return 0;
}

This may print:

Less or equal
ch value is -121

Use an usigned type or an int for the calculation.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63