0

i am failing to see my fault in logic .... if i run ./substitution qwertyuioplkjhgfdsazxcvbnm and input plaintext: abc
instead on ciphertext: qwe i get ciphertext: dvm ...i tried debug50 and it seems there are more cases when the 'if' applies ...but i don`t understand how that is possible.....

    //generate an array of uppercase letters of the alphabet
    //for lowercase letters just add 32 to each item
    int upperalpha[N];
    for (int i = 'A'; i <= 'Z'; i++)
        {
            upperalpha[i - 'A'] = i;
        }

    //get message from the user
    string plaintext = get_string("plaintext: ");

    //encript the message
    for (int i = 0; i < strlen(plaintext); i++)
    {
        for (int  j = 0; j < N; j++)
        {
            if (plaintext[i] == upperalpha[j] || plaintext[i] == upperalpha[j] + 32)
            {
                plaintext[i] = argv[1][j];
            }
        }
    }


        //output the cripded message
        printf("ciphertext: %s\n", plaintext);
        return 0;
0

it works if it put a break after each if statement ....i still don`t understand why this works.....

below is my final implementation of the substitution problem ...it passes check50 ...but i still do not understand why i need the break...i would be grateful to anyone who could enlighten me :D

    //generate an array of uppercase letters of the alphabet
    //for lowercase letters just add 32 to each item)
    char upalpha[26];
    for (int a = 'A'; a <= 'Z'; a++)
    {
        upalpha[a - 'A'] = a;
    }
    

    //get message from the user
    string plaintext = get_string("plaintext: ");

    //encript the message
    int lenght = strlen(plaintext);
    for (int b = 0; b < lenght; b++)
    {
        for (int  c = 0; c < 26; c++)
        {
            if (plaintext[b] == upalpha[c] || plaintext[b] == (upalpha[c] + 32))
            {
                //if both plaintext and key are the same case (lower or uppercase) mentain case
                if ((isupper(plaintext[b]) && isupper(argv[1][c])) || (islower(plaintext[b]) && islower(argv[1][c])))
                {
                    plaintext[b] = argv[1][c];
                    break;
                }
                
                // if plaintext is uppercase and key is lowercase make ciphertext uppercase
                else if (isupper(plaintext[b]) && islower(argv[1][c]))
                {
                    plaintext[b] = argv[1][c] - 32;
                    break;
                }
                
                //if plaintext is lowercase and key is uppercase make ciphertext lowercase
                else if (islower(plaintext[b]) && isupper(argv[1][c]))
                {
                    plaintext[b] = argv[1][c] + 32;
                    break;
                }
            }
        }
    }
    
    //output cripted message
    printf("ciphertext: %s\n", plaintext);
    return 0;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .