0

Don't get the problem why some letters from my keyword are not interpreted as letters (alpha).

Here my code. To try to debug it instead of printing the encrypted phrase i print the table (see below the output):

            //encrypt the text and print it
        int ascii_a = 97; //can be called just 'a'
        int ascii_A = 65; //can be called just 'A'
        for (int i = 0, n = strlen(p); i < n; i++)
        {
            int key_k = 0;
            if (isalpha(p[i]) && islower(p[i])) //test if the char_i in key is a letter and if its upper or lower
            {
                if (isalpha(key[i]) && islower(key[i]))
                    key_k = key[i] - ascii_a;
                else if (isalpha(k[i]) && isupper(k[i]))
                    key_k = key[i] - ascii_A;

                printf("%c", (p[i] - ascii_a + key_k) % 26 + ascii_a);
            }   
            else if (isalpha(p[i]) && isupper(p[i])) ////test if the char_i in key is a letter and if its upper or lower
            {   
                if (isalpha(key[i]) && islower(key[i]))
                    key_k = key[i] - ascii_a;
                else if (isalpha(k[i]) && isupper(k[i]))
                    key_k = key[i] - ascii_A;

                //printf("%c-%d_", (p[i] - ascii_A + key_k) % 26 + ascii_A, key_k); //used for encrypted message
                printf("%c - %c - %d - %d - %d\n", p[i], key[i], key[i], key_k, isalpha(k[i])); //the testing table
            }   
            else
                printf("%c", (p[i]));

Text for testing is: TESTTESTTEST Text for key is: BAZBAZBAZBAZ (keyword is just BAZ)

So the output:

T - B - 66 - 1 - 1024
E - A - 65 - 0 - 1024
S - Z - 90 - 25 - 1024
T - B - 66 - 0 - 0 !!!why is it 0??? 
T - A - 65 - 0 - 1024
E - Z - 90 - 25 - 1024
S - B - 66 - 1 - 1024
T - A - 65 - 0 - 1024
T - Z - 90 - 25 - 1024
E - B - 66 - 1 - 1024
S - A - 65 - 0 - 0 !!!why is it 0??? 
T - Z - 90 - 25 - 1024
4
  • And btw: it works properly if you don't use isalpha() and islower-isupper() conditions and use if (key[i]>96 && key[i]<123) instead Jul 21 '17 at 11:02
  • 1
    What is k, what is key? What happens if i is >=strlen(key)? Also, needs different index for key, as the position in the key advances only on encrypting a letter.
    – Blauelf
    Jul 21 '17 at 11:57
  • k is a word that user sets (BAZ in example). key is a string which is set to be always a length of the phrase (p). Like in example with bacon where there was a phrase like "bac onbac on baconba". So i cannot be >= strlen(key) and this allows me to avoid double loop. Jul 22 '17 at 9:50
  • On the conditions in the innermost ifs, you use key on the if condition and k on the else if condition.
    – ChrisG
    Jul 22 '17 at 12:30

You must log in to answer this question.

Browse other questions tagged .