0

it works for the lenth of key word but looses track from there have run a debug50 over it and plain text and key seem to increment fine key is showing a wrap around so a bit confused if you know please i dont want the answer but a pointer where to look so i can fix myself kind regards cosscoss

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

int main(int argc, char *argv[])
{
    int j =0;//index for key
    string k = argv[1];//place holder for key
    string s;//place holder for plain text 
    if(argc != 2)//checks if two arguments provided
    {
        printf(" need 2 command line arguments please !\n");
        return 1;//close program
    }
    for(int p = 0, n = strlen(k); p < n; p++)// iterates over key 
    {
         if( ! isalpha(k[p])) // checks if alpha
        {
             printf("only alpha please\n");
             return 1;
        }
    }
    int kl = strlen(k); //place holder for lenth of key
    {   
        printf("plaintext: ");
        s = get_string(" ");//gets input from user
        printf("ciphertext: ");
    }
     for(int i =0, m = strlen(s); i < m; i++)//iterates over elements of string
        {
                if(isupper(s[i])) // checks if upper case
                {
                     k[j] = k[j] -'A';
                     s[i] = s[i] - 'A';// converts index of key to 0/25 scale
                     s[i] = (s[i] + k[j]);
                     s[i] = s[i] %26;// adds index of plain text and key text mod key mod 26
                     printf("%c",s[i] +'A');// converts result of previous line back to ascii and prints it
                     j++ ;
                     j = j % kl;
                }
                else if(islower (s[i]))
                {
                    //k[j] = k[j] -'a';
                    s[i] = s[i] - 'a'; //converts index of plain text to 0/25 scale
                    k[j] = k[j] -'a';
                    s[i] = (s[i] + k[j]);
                    s[i] = s[i] % 26 ;// adds index of plain text and key text mod key mod 26
                    s[i] =s[i] + 'a';
                    printf("%c",s[i]);// converts result of previous line back to ascii and prints it
                    j++ ;
                    j = j % kl;

                }
                else
                {
                    printf("%c",s[i]);//prints non alpha
                }      
        }
         printf("\n");
         return 0;
}

1 Answer 1

0

Think about what happens to the key chars when the code is using the key the second time through.

For purposes of simplicity, assume everything is upper case for now. On the first pass through the key, the following code will be executed:

  if(isupper(s[i])) // checks if upper case
            {
                 k[j] = k[j] -'A';

The ASCII code of k[j] is replaced by the appropriate number from 0 to 25. For example, B, or ASCII 66 becomes 1.

Now, fast forward to the next pass through the key. k[j] now has 'A' subtracted again and becomes -64. On the third pass, it would be -129, and so on.

Because of this, the code will only work correctly on the first pass through. You asked for a pointer and not a solution, but it was necessary to explain what was happening. However, since it isn't a simple solution, you'll have plenty of time to rethink things. ;-)

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

2
  • thank you so much
    – cosscoss
    Sep 23, 2017 at 12:06
  • else if(islower (s[i])) { k[j] = k[j] -'a'; s[i] = s[i] -'a'; //converts index of plain text to 0/25 scale s[i] = (s[i] + k[j]);// adds index of plain text and key text mod key mod 26 s[i] =s[i] %26; k[j] = k[j] +'a'; printf("%c",s[i]+'a');// converts result of previous line back to ascii and prints it j++ ;
    – cosscoss
    Sep 23, 2017 at 12:17

You must log in to answer this question.

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