title: CS50 Problem Set 2: Substitution (Need Help)
body: <p>Im facing some issue here. Can anyone tell me what is wrong with my code?</p>
  <p><strong>This is the check50 result:</strong></p>
  <p>:) substitution.c exists</p>
  <p>:) substitution.c compiles</p>
  <p>:( encrypts &quot;A&quot; as &quot;Z&quot; using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
  expected &quot;ciphertext: Z...&quot;, not &quot;&quot;</p>
  <p>:( encrypts &quot;a&quot; as &quot;z&quot; using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
  expected &quot;ciphertext: z...&quot;, not &quot;&quot;</p>
  <p>:( encrypts &quot;ABC&quot; as &quot;NJQ&quot; using NJQSUYBRXMOPFTHZVAWCGILKED as key
  expected &quot;ciphertext: NJ...&quot;, not &quot;&quot;</p>
  <p>:( encrypts &quot;XyZ&quot; as &quot;KeD&quot; using NJQSUYBRXMOPFTHZVAWCGILKED as key
  expected &quot;ciphertext: Ke...&quot;, not &quot;&quot;</p>
  <p>:( encrypts &quot;This is CS50&quot; as &quot;Cbah ah KH50&quot; using YUKFRNLBAVMWZTEOGXHCIPJSQD as key
  expected &quot;ciphertext: Cb...&quot;, not &quot;&quot;</p>
  <p>:( encrypts &quot;This is CS50&quot; as &quot;Cbah ah KH50&quot; using yukfrnlbavmwzteogxhcipjsqd as key
  expected &quot;ciphertext: Cb...&quot;, not &quot;&quot;</p>
  <p>:( encrypts &quot;This is CS50&quot; as &quot;Cbah ah KH50&quot; using YUKFRNLBAVMWZteogxhcipjsqd as key
  expected &quot;ciphertext: Cb...&quot;, not &quot;&quot;</p>
  <p>:( encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
  expected &quot;ciphertext: Rq...&quot;, not &quot;&quot;</p>
  <p>:( does not encrypt non-alphabetical characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
  expected &quot;ciphertext: Yq...&quot;, not &quot;&quot;</p>
  <p>:) handles lack of key</p>
  <p>:) handles too many arguments</p>
  <p>:) handles invalid key length</p>
  <p>:) handles invalid characters in key</p>
  <p>:) handles duplicate characters in key</p>
  <p>:) handles multiple duplicate characters in key</p>
  <p><strong>This is my code:</strong></p>
  <pre><code>    #include &lt;cs50.h&gt;
  
      #include &lt;stdio.h&gt;
  
      #include &lt;ctype.h&gt;
  
      #include &lt;string.h&gt;
  
      int main(int argc, string argv[])
  
     {
          string alphabet= &quot;abcdefghijklmnopqrstuvwxyz&quot;;
  
  if(argc != 2)
  {
      printf(&quot;missing/more than 1 command-line argument\n&quot;);        
      return 1;
  }
  
  //check if there are 26 characters
  int a= strlen(argv[1]);
  
  if(a!=26)
  {
      printf(&quot;key must contain 26 characters\n&quot;);
      return 1;
  }
  
  
  //Check if characters are all alphabetic
  for(int i=0, n=strlen(argv[1]); i&lt;n; i++)  
  {
      if(!isalpha(argv[1][i])) 
      {
          printf(&quot;only alphabetic characters allowed\n&quot;);
          return 1;
      }
  
      //check if each letter appear only once
  
      for(int j=1; j&lt;n; j++)
      {
          if(argv[1][i]==argv[1][j])  
          {
              printf(&quot;repeated alphabets not allowed\n&quot;);
              return 1;
          }
          }
  }
  
  
  //prompt user for plaintext
  
  string b= get_string(&quot;plaintext: \n&quot;);
  
  int m=strlen(b);
  
  char ciphertxt[m+1];   
  
  
  //find out the alphabetical position of each character in string b (i.e character c in string b has alphabetical position of 3)
  
  for(int k=0; k&lt;m; k++)
  {
      for(int p=0, q=strlen(alphabet); p&lt;q; p++)
      {
          if(b[k]==alphabet[p])
          {
              ciphertxt[k]= tolower(argv[1][p]);
  
              break;                                
      }
  
      else if(b[k]==(alphabet[p]-32))
      {
          ciphertxt[k]= toupper(argv[1][p]);
          break;
      }
  
      else
      {
          ciphertxt[k]= b[k];
      }
  
  }
  
      }
  
      ciphertxt[m]='\0';
  
      //print ciphertext
  
  printf(&quot;ciphertext: %s\n&quot;, ciphertxt);
  return 0;
      }
  </code></pre>