• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #164
    admin
    Keymaster
      
      
      #include<stdio.h>
      #include<cs50.h>
      #include<string.h>
      #include<ctype.h>
      int main(void)
      {
      string name = get_string("Enter: ");
      printf("Entered text by user: %s\n", name);
      int n = strlen(name);
      printf("length of entered text: %i\n", n);
      int key = get_int("enter key: ");
      char newuppercase_array[n];
          for (int i = 0; i < n; i++)
          {
              if (isupper(name))
              {
              newuppercase_array = ((((int newuppercase_array - 65) + key)%26) + 65);
              printf("ciphertext %c",newuppercase_array[]);
              }
              else
              {
              newuppercase_array = newuppercase_array;
              }
          }
      }

      In line 17:

      newuppercase_array = ((((int newuppercase_array – 65) + key)%26) + 65);

      There is a parenthesis error (https://www.canva.com/design/DAE2bi-j2DQ/MztV_c1ft77ouCAv6J_tjA/view?utm_content=DAE2bi-j2DQ&utm_campaign=designshare&utm_medium=link&utm_source=sharebutton). Help appreciated.


      Reply


      If you want to cast a value as ‘int’ the format is: (int) value

      The way you wrote it it looks like you are attempting to declare a new variable 🙂


      Query


      Revised:

      newuppercase_array = (((((int) newuppercase_array – 65) + key)%26) + 65);


      
      
      #include<stdio.h>
      #include<cs50.h>
      #include<string.h>
      #include<ctype.h>
      int main(void)
      {
      string name = get_string("Enter: ");
      printf("Entered text by user: %s\n", name);
      int n = strlen(name);
      printf("length of entered text: %i\n", n);
      int key = get_int("enter key: ");
      char newuppercase_array[n];
          for (int i = 0; i < n; i++)
          {
              if (isupper(name))
              {
              newuppercase_array = (((((int) newuppercase_array - 65) + key)%26) + 65);
              printf("ciphertext %i",newuppercase_array);
              }
              else
              {
              newuppercase_array = newuppercase_array;
              }
          }
      }

       

      Not getting the correct ciphertext.

       


      Reply


      `newuppercase_array = (((((int) newuppercase_array – 65) + key)%26) + 65);

      1. Converting to int here does nothing

      2. You are ciphering the value of newuppercase_array (which isn’t initialized yet) rather than the character in name.


      Query


       

      For the second point raised by you, this is one more area where I am struggling.

      Will it be:

      newuppercase_array = (((((int) name – 65) + key)%26) + 65);

      Before that, do I still need to create a new array as it appears name array yet not in existence.


      Just checked putting

      newuppercase_array = (((((int) name – 65) + key)%26) + 65);

      and it is giving correct output for uppercase.

      However, not getting any ouput other than cipher text of uppercase characters.

      
      
      for (int i = 0; i < n; i++)
      {
          if (isupper(name))
          {
          newuppercase_array = ((((name - 65) + key)%26) + 65);
          printf("ciphertext %c",newuppercase_array);
          }
          else
          {
          newuppercase_array = name;
          }
      }
      
      

       

      printf(“%c”,newuppercase_array);

      The above giving output the way I desire.

      [learn_press_profile]

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