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

int main (int argc, string argv[])
{

  string k= argv[1];
  string p = argv[2];

  if (argc==2) 
    {
      p=GetString();
    }
  else
    {
      printf("There is an error in input");
      return 1;
    }

  for (int a = 0, m = strlen(k); a<m; a++)
    { 
      if ((k[a] > 'Z'&& k[a] < 'a' )|| (k[a]> 'z')|| (k[a]< 'A'))
        {
          printf("This is an invalid entry\n");
          return 1;
        }
    }      

  int a = 0;      

  for (int i = 0, n = strlen(p); i<n; i++)
    {
      int m = strlen(k);

      if((p[i] > 'Z'&& p[i] < 'a' )|| (p[i]> 'z')|| (p[i]< 'A'))
        {
          printf("%c",p[i]);
        }

      else
        {

          if (k[a] >=65 && k[a] <= 90)

            {
              (k[a] = k[a] - 65);
            }

          if (k[a] >=97 && k[a] <= 122)

            {
              (k[a] = k[a] - 97);
            }

          if((p[i] <= 'Z'&& p[i] >= 'A' ) && (p[i]+k[a] <= 'z'&& p[i]+k[a] >= 'a')) 
            {
              (p[i] = p[i]+ k[a] -26);
              printf("%c",p[i]);
            }

          else if((p[i]+k[a] <= 'Z'&& p[i]+k[a] >= 'A' )|| (p[i]+k[a] <= 'z'&& p[i]+k[a] >= 'a')) 
            {
              (p[i] = p[i]+ k[a]);
              printf("%c",p[i]);
            }

          else
            {

              (p[i]= p[i]+k[a]-26);
              printf("%c",p[i]);
            }

          a++;

          if (a>m)
            {
              (a= 0);
            }


        }
    }
  printf("\n");

  return 0; 
}

Please help, I have spent hours being stuck on this one last step for pset2.

Shown in the picture is part of my code for vigenere. What I am stuck on is that argv[2] cannot be successfully translated into the secret code I want because somehow not the whole "k" string (k string is my key string from argv[1]) is being read and executed. I've read my code over and over again but still can't find what is wrong.

Can someone please help me T_T

Thanks

0

For starters, why are you trying to access argv[2]? What are you using it for? The problem spec says that argv[1] is the key string and you are to prompt for a string to be encoded. Also, the part of your code dealing with argv isn't shown. It makes it difficult to offer useful assistance.

Please spend some time with the help tab above to learn how to post formatted code, rather than screen captures of code. IF you can't cut and paste the code directly from the appliance, you should still be able to set up a Dropbox folder on your computer outside of the appliance so that you can cut and paste from there instead.

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

1
  • Hi Cliff, I have just changed my images to code blocks, please let me know if this is easier to read. Thanks for offering the help. It is much appreciated!
    – Leon
    Nov 15 '15 at 23:37

You must log in to answer this question.

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