0

When executed, my code prints out only 1 letter of the plaintext edited. Logically, it seems to be correct. Any help would be greatly appreciated. Thanks in advance :)

# include <stdio.h>
# include <cs50.h>
# include <string.h>
# include <stdlib.h>
# include <ctype.h>


int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf ("Kindly enter 1 argument after the file name\n");
        return 1;
    }

    string w = argv[1];
    int j = 0;
    string p = GetString();

    for (int i = 0; i < strlen(p); i++)
    {

        // int lj;
        // int sj;

        while (j <= i && isalpha(p[i]))
        {
            if (isupper (w[i % strlen(w)]))
            {
                int su = (w[i % strlen(w)] - 65) % 26;
                if (isupper (p[i]))
                {
                    int u = (p[i] + su - 64) % 26 + 64;
                    printf ("%c", (char) u);
                }
                else if (islower (p[i]))
                {
                    int l = (p[i]+ su - 96)% 26 + 96;
                    printf ("%c", (char) l);
                }
                else 
                {
                    printf ("%c",p[i]);
                }
            }
            else if (islower (w[i % strlen(w)]))
            {
                int sl = (w[i % strlen(w)] - 97) % 26;
                if (isupper (p[i]))
                {
                    int r = (p[i] + sl - 64) % 26 + 64;
                    printf ("%c", (char) r);
                }
                else if (islower (p[i]))
                {
                    int v = (p[i]+ sl - 96)% 26 + 96;
                    printf ("%c", (char) v);
                }
                else 
                {
                    printf ("%c",p[i]);
                }
                j = j + 1;
            }
        }
    }
    printf ("\n");
    return 0;
}
2
  • your code is now more readable and it was missing with main ending }.
    – Wafeeq
    Jul 7 '16 at 11:18
  • When executed, my code prints out only 1 letter of the plaintext edited it is not true, your program Outputs not only signle letter. explain your question more.
    – Wafeeq
    Jul 7 '16 at 11:32
1

Instead of printing non-alphas without encoding them, this code is printing the next encoded letter for each preceeding non-alpha. In other words, if the key was a (meaning don't encode, just echo everything), and the string was b12c . de it would print bcccdddde. While I've never seen this effect, the problem is a common one. The index for the key, j, is tied to the index for the plain text, i.

The source of this problem is the inner while loop. It both tries to control the key with a loop and to indirectly tie the two indexes together. Simply put, the index for the key needs to be totally independent of the index for the plain text. It should be incremented when and only when an alpha is encoded, and checked to make sure it doesn't exceed key length.

This tells you what is wrong. Given the level of skill demonstrated in your code, you should be able to figure out how to implement the changes. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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