• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #242
    admin
    Keymaster

      For converting to uppercase/lowercase for checking uniqueness of keys, do I need to create a function?

      I have the following code in the main function that converts all keys to uppercase as part of checking the uniqueness of keys.

      argv[1][j] = toupper(argv[1][j]);

      Since keys are converted uppercase, there is no way to recover what the user actually entered as uppercase/lowercase.


      Reply


      No need to convert anything but can compare directly.

      Try not to over complicate it. Keep it simple 🙂 Figure out how you can compare if ‘A’ and ‘a’ is the same letter.


      I tried with the following code to check if the user enters uppercase or lowercase characters as part of plaintext:

      
      
      for (int i = 0; i < countstring; i++) //starts with first character of plaintext till the end
      {
          if (isupper s)
          {
          s = argv[1] - 65];
          }
          else
          {
          s = argv[1] - 97];
          }
      }
      
      

      Here is the full program:

      
      
      #include<stdio.h>
      #include<cs50.h>
      #include<string.h>
      #include<ctype.h>
      #include<stdlib.h>
      int main(int argc, string argv[])
      {
      int counter = 0;
          if (argc != 2)
          {
          return 1;
          }
      string key = argv[1];
      int t = strlen(key);
          if (t != 26)
          {
          printf("Usage: ./substitution key");
          return 1;
          }
          while (counter < t)
          {
              if (!isalpha(argv[1][counter]))
              {
              printf("enter only alphabetic characters");
              return 1;
              }
              else
              {
              counter = counter + 1;
              }
          }
          for (int i = 0; i < 25; i++)
          {
              for (int j = i; j < 25; j++)
              {
              argv[1][j] = toupper(argv[1][j]);
              printf("capital%c", argv[1][j]);
                  if (argv[1][j] == argv[1][j + 1])
                  {
                  printf("keys should be unique");
                  return 1;
                  }
          }
      }
      string s = get_string("plaintext: ");
      int countstring = strlen(s);//count the number of characters entered by the user as plaintext
      printf("number of plaintext characters: %i", countstring);
          for (int i = 0; i < countstring; i++) //starts with first character of plaintext till the end
          {
          if (isupper s)
          {
          s = argv[1] - 65];
          }
          else
          {
          s = argv[1] - 97];
          }
      }
      printf("ciphertext: %s", s);
      }

       

      Here is the error screenshot:

       


      Reply


      if (isupper s)

      The correct syntax is:

       

      The error message tells you that isupper without the argument in parentheses will always evaluate to true. When you get an error message like that, even if you don’t understand the exact meaning, it points you in the direction where to look 🙂


      Query


      Thanks for the continued support. After revising, I fail to understand why else conditoin below not converting to lowercase of ciphertext.

      
      
      for (int i = 0; i < countstring; i++) //starts with first character of plaintext till the end
      {
          if (isupper (s))
          {
          s = argv[1] - 65];
          }
          else
          {
          s = argv[1] - 97];
          }
      }

       

       


      Reply


      This line finds the correct alphabetic position of a lowercase letter in plaintext and substitutes with the corresponding position in the key.

      So if the letter in the key is uppercase, you are simply using that uppercase letter. You will need to make sure that the case of the cipher letter is the same case as the letter in plaintext.

      [learn_press_profile]

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Scroll to Top