• Skip to main content
  • Skip to footer

SoftwareProg.com

Software programming learning aide

  • Blog
    • Discussion
  • News
  • CS50’s Introduction to Computer Science by Harvard University on Edx
    • Week 1
    • Week 2
    • Week 3
    • Week 4
    • Week 5

Do I need to create a function while converting keys to uppercase/lowercase for checking uniqueness

CS50 threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2 › Do I need to create a function while converting keys to uppercase/lowercase for checking uniqueness

Tagged: pseudocode of substitution project, substitution project

  • This topic is empty.
Log In Register Lost Password
Viewing 1 post (of 1 total)
  • Author
    Posts
  • February 23, 2022 at 1:07 pm #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:


      Copy Code
      Copied
      Use a different Browser

      
      
      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:


      Copy Code
      Copied
      Use a different Browser

      
      
      #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:


      Copy Code
      Copied
      Use a different Browser

      
      
      isupper(s)

       

      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.


      Copy Code
      Copied
      Use a different Browser

      
      
      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



      Copy Code
      Copied
      Use a different Browser

      
      
      s = argv[1] - 97];

      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]

    • Author
      Posts
    Log In Register Lost Password
    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Log In

    Log in / Register

    Initiative by Digital Splendid OPC Pvt. Ltd.