• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #199
    admin
    Keymaster
      
      
      #include<stdio.h>
      #include<cs50.h>
      #include<ctype.h>
      #include<string.h>
      #include<stdlib.h>
      
      int main(int argc, string argv[])
      {
          if (argc!=2)
          {
          printf("enter key value");
          return 0;
          }
      string enteredtext = get_string("plain text:");
      printf("Entered text by user: %s\n", enteredtext);
      int n = strlen(enteredtext);
      printf("length of entered text: %i\n", n);
      string key = argv[1];
      int keylen = strlen(key);
      int counter = 0;
          while(counter !> argv[1][keylen])
          {
              if(isalpha (argv[1][0]));
              {
              printf("key value should be integer");
              return 0;
              }
              else
              {
              counter = counter + 1;
              }
          }
      int convertedkey = atoi(argv[1]);
          for (int i = 0; i < n; i++)
          {
              if (isupper(enteredtext))
              {
              enteredtext = ((((enteredtext - 65) + convertedkey)%26) + 65);
              }
              else if (islower(enteredtext))
              {
              enteredtext = ((((enteredtext - 97) + convertedkey)%26) + 97);
              }
              else
              {
              enteredtext = enteredtext;
              }
              printf("%c", enteredtext);
          }
      printf("\n");
      }

      https://www.canva.com/design/DAE3jkZvhZM/QiwpjHlonPbXQOxfw6oOFw/view?utm_content=DAE3jkZvhZM&utm_campaign=designshare&utm_medium=link&utm_source=sharebutton

      Any clue appreciated particularly for:

      
      
      int counter = 0;
      while(counter !> argv[1][keylen])
      {
          if(isalpha (argv[1][0]));
          {
          printf("key value should be integer");
          return 0;
          }
          else
          {
          counter = counter + 1;
          }
      }

      Reply

      int counter = 0; //counter is initialized as 0.

      while(counter !> argv[1][keylen]) while loop supposedly runs until when? should be like while(counter < keylen)

      {

      if(isalpha (argv[1][0])); //argv[1][0]. Only checks the first char of argv[1] string. should be something like argv[1][counter]

      {

      printf(“key value should be integer”);

      return 0;

      }

      else

      {

      counter = counter + 1;

      }

      }

      There might be more issues but for now let you get through these.


      In this case OP only checks if the key contains alpha characters. What he should check is if the key contains anything else than digits. The function isdigit() is useful for this.


      Exactly what I realized when I was doing the same problem and went into sea of punctuations.

       [learn_press_profile]

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