title: Problem With Duplicated Characters In A String
body: <p>I'm just writing a code in C that encrypts string using a key entered by user. The key entered by user has 4 requirements:</p>
  
  <ul>
  <li>It must be a single word</li>
  <li>It must be of 26 characters</li>
  <li>It can contain only alphabetic characters</li>
  <li>It can't contain duplicated characters</li>
  </ul>
  
  <p>The problem is with the fourth requirement, as the duplicate check not always works properly (I tried with the key YFDTSMPBVIERGHEWONUAKLQXCZ that it contains 2 times the letter "E", and this key is accepted by the program). I'd like to know what could be the problem if you can help; down here you can find the code of the program:</p>
  
  <pre><code>#include &lt;cs50.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  
  int main(int argc, string argv[])
  
  {
  
      //Check if there is a number of command-line arguments different than 1
      //And return an error message
      if (argc != 2)
      {
          printf("Usage: ./substitution KEY\n");
          return 1;
      }
  
      //Check if the lenght of the string is  more or less than 26 characters
      //And return an error message
      else if (strlen(argv[1]) != 26)
      {
          printf("Key must contain 26 characters\n");
          return 1;
      }
  
      else
      {
  
          //Check if the command-line argument is alphabetic, and compare the
          //Number of characters in the argument with the argument lenght; if
          //The two  values correspond,  then the string  is considered valid
          //Otherwise the system will return an error message
          int index = 0, j;
          string s = argv[1];
  
          for (j = 0; s[j] != '\0'; j++)
          {
              if ((s[j] &gt;= 'A' &amp;&amp; s[j] &lt;= 'Z') || (s[j] &gt;= 'a' &amp;&amp; s[j] &lt;= 'z'))
  
              {
                  index++;
              }
          }
  
          if (strlen(argv[1]) != index)
          {
              printf("Key must only contain alphabetic characters\n");
              return 1;
          }
  
          else
          {
  
              //Check if the command-line  argument contains characters that
              //Are repeated;  in this case the system  will return an error
              //Message
              int count;
              for (int z = 0; z &lt; strlen(s); z++)
              {
                  count = 1;
  
                  for (j = z + 1; j &lt; strlen(s); j++)
                  {
  
                      if (s[z] == s[j] &amp;&amp; s[z] != ' ')
                      {
                          count++;
                      }
  
                  }
                  if (count &gt; 1 &amp;&amp; s[z] != '0')
                  {
                      printf("Key must not contain repeated characters\n");
                      return 1;
                  }
  
                  else
                  {
  
                      //Ask the user for a string to be encrypted
                      string plaintext = get_string("plaintext : ");
  
                      {
                          printf("ciphertext: ");
                      }
  
  
                      { 
                          //Starts loop check into plaintext
                          for (int i = 0; i &lt; strlen(plaintext); i++)
  
                          {
                              //Start loop check into key
                              for (int jax = 0; jax &lt; 26; jax++)
  
                              {
                                  //Check coherence between lower cases in the same character of both plaintext and key
                                  if (islower(
                                          plaintext[i]))
  
                                  {
                                      if (plaintext[i] == 'a' + jax)
  
                                      {
                                          printf("%c", tolower(s[jax]));
                                      }
  
                                  }
                                  //Check coherence between lower cases in the same character of both plaintext and key
                                  else if (isupper(
                                               plaintext[i]))
                                  {
                                      if (plaintext[i] == 'A' + jax)
                                      {
                                          printf("%c", toupper(s[jax]));
                                      }
                                  }
                              }
  
                              //If it is not an alphabetical character, just print  it as
                              //is on screen
                              if (!isalpha(plaintext[i]))
                              {
                                  printf("%c", plaintext[i]);
                              }
                          }
                          printf("\n");
                          return 0;
                      }
                  }
              }
          }
      }
  }
  </code></pre>