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

    //Declare variables


    //check user's input value
    int main (int argc, string argv[])
    {
        if(argc != 2)
         {
          printf("Please insert ONE key\n");
          return 1;
         }

         for (int i = 0, n = strlen(argv[1]); i < n; i++)
          {
           if(!isalpha(argv[1][i]))
            {
             printf("Please insert an ALPHABETICAL key\n");
             return 1;
            }
          }

        //Get the text
        string key = argv[1];;
        string text = GetString();
        int keylen = strlen(key);

            //cipher
       int k  ;
        for(int j = 0, l = strlen(text); j < l; j++)
         {



                  k = j % keylen ; 
                 if(isalpha(text[j]))
                  {

                    if(isupper(text[j]))
                     {


                      printf("%c", (((text[j] - 'A') + (key[k] - 'A')) % 26) +'A');

                     }else
                     {

                     printf("%c", (((text[j] - 'a') + (key[k] - 'a')) % 26) +'a');
                      k ++ ;
                     }
                    }
                    else
                    {
                 printf("%c", text[j]);

                    }
                printf("\n");

                return 0;

              }
            }
0

You never move through the key. k is never incremented. It is only initialized at the beginning to 0. Also, you don't handle the scenario where the case of the key and the plain text are not the same.

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

2
  • I increament key with for (p = 0 ; k < keylen ; p ++) but doesnot work this my solution to the first prob Apr 19 '16 at 10:01
  • actually, you don't increment k. You increment p. (btw, the line in your comment with k<keylen doesn't exist in the code that you posted. Did you post the right version of the code? It also looks like there's a big cut and paste error near the end.) Earlier in your code, you have the line int k = p % keylen ; This creates k and initializes it to 0. It does not create a permanent association between p and k. After this, there is no code that alters the value of k. Also, the % operation effectively does nothing, since p is 0 at that point. k must increment when a letter is encoded.
    – Cliff B
    Apr 19 '16 at 16:09

You must log in to answer this question.

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