• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #153
    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("lenght of entered text: %i\n", n);
        int key = get_int("enter key: ");
        for (int i = 0; i < n; i++) 
        {
          if (isalpha(name)) 
          {
            if (name + key >= 91) 
            {
              name = (name + key - 26);
            } 
            else 
            {
              name = (name + key);
            }
          } 
          else 
          {
            name = name;
          }
        }
        printf("cipher text: %s\n", name);
      }

      Above is my code for uppercase characters (I am assuming for the time being the user enters only uppercase characters and not lowercase) only which is still faulty (https://www.canva.com/design/DAE1xXhyoXk/ba5X0Pf7gSAkmh9LdkbuOQ/view?utm_content=DAE1xXhyoXk&utm_campaign=designshare&utm_medium=link&utm_source=sharebutton).

      Up to key 44 or {72 which is ASCII of H + 44) = 116, the code working. So now perhaps need to introduce % operator correctly for rotation between 65 to 90.


      Reply


      1 % 26 = 1 , essentially if k is < n:

      k % n = k

      But

      How about n % n = ?

      Or: n + 1 % n = ? n + 2 % n = ? n + 3 % n = ? …

      You should even try out: 2n % n 2n + 1 % n …

      Try it out! See the pattern? Good luck!


      Thanks.

      Though still far from coming out with the pattern, here is my intermittent code:

      
      
      #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("lenght of entered text: %i\n", n);
        int key = get_int("enter key: ");
        for (int i = 0; i < n; i++)
        {
          if (isalpha(name))
          {
            if (name + key > 90 && name + key < 143)
            {
              name = (name + key - 26);
            }
            if (name + key > 142 && name = (name + key - 52);
            } 
            else
            {
              name = (name + key);
            }
          } 
          else
          {
            name = name;
          }
        }
        printf("cipher text: %s\n", name);
      }

      My intention was to make the code work up to cycle that begins with 143 up to 194. I am surprised to see that up to key 18, the code works (z cipher text). But as I enter key 19, I get T as cipher text:

      Instead of if (isalpha(name)), can isalpha be replaced by a CS50 function that will spot instead of letters, uppercase letters/lowercase letters. I tried with if (uppercase(name)), but did not work.


      Reply


      Yes isupper, and islower under I believe the ctype library

      Use this next time, and remember, they give you clues on the pset itself.

      https://manual.cs50.io/


      Reply


      https://edstem.org/us/courses/176/discussion/1004831?comment=2311979

       [learn_press_profile]

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