0

Could I get some help with my code? For some reason, the program ignores conditions and prints the wrong letter. For example, I would use the letter i as the key to convert Meet. It would come out as UmmX. If I use f, I would get Rjjy. The program only skips through the condition when it gets to the letter i and beyond. It would go through if(argv[1][j] > 96 && argv[1][j] < 123) for the first three letters and then skip to the else on the "t" even though it meets the if condition. I don't know why this is happening.

  int main(int argc,string argv[])
  {
if(argc !=2)
{
    printf(" Need One More\n");
    return 1;
}    
int j = 0;
string plaintext = GetString();
int n = strlen(plaintext);
int m = strlen(argv[1]);
for(int i =0; i < n; i++)
{
        if(isdigit(plaintext[i]) || plaintext[i] == 32 || plaintext[i] < 65 || (plaintext[i] > 90 && plaintext[i] < 97 )|| plaintext[i] > 122)
        {
         plaintext[i] = plaintext[i];
         printf("%c", plaintext[i]);
        }
         if( j == m)
         {
          j = 0;
         }


      if(argv[1][j] > 96 && argv[1][j] < 123)
       {
          int leftover = argv[1][j] - 97;
            if(plaintext[i] + leftover < 123)
            {
                plaintext[i] = plaintext[i] + leftover;
                printf("%c", plaintext[i]);

                j++;
            }
            else
                {
                plaintext[i] = 97+ (leftover + argv[1][j] - 122) ;
                printf("%c", plaintext[i]);

                j++;

            }    
       }
       else if(argv[1][j] < 91 && argv[1][j] > 64) 
       {
          int leftover = argv[1][j] - 65;
          if(plaintext[i] + leftover < 91)
            {
           plaintext[i] = plaintext[i] + leftover;
            printf("%c", plaintext[i]);

             j++;

            }
            else
            {
            plaintext[i] = 65+ (leftover + argv[1][j] - 65);
            printf("%c", plaintext[i]);

            j++;

            }    
       }




}
printf("\n");

1 Answer 1

0

plaintext = 't' key = 'i' (9)

It will skip the if block because 't' + 9 == 123

plaintext[i] = 97+ (leftover + argv[1][j] - 122) ;

Where is the plaintext taken into account in this equation? "leftover" is the key minus the ASCII offset, argv[1][j] is the full key (why?), 122 is some magic constant. You're not taking the plaintext into account at all.

Also, some tips: 1) Do not modify the original plaintext string at all. You can run into some weird bugs, depending on how you run the calculations. And there's no need to modify it. You only need to print one character at a time.

2) Since you're already using the ctype.h library for isdigit, start using its functions in order to tell whether a character is alphabetic, or upper, or lower case. It will make your code more readable and much less error prone.

3) You're not considering the differences between an upper or lower case plaintext, only the key.

You must log in to answer this question.

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