0

Here is my code which works well even with non-numeric key.

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
bool key_check(string s);
int main(int argc, string argv[])
{
    int i, j;
    if (argc == 2)
    {
    if (!key_check(argv[1]))
    {
      printf("Usage: ./caesar key");
      return 1;
    }
    }
    else if (argc != 2)
    {
      printf("Usage: ./caesar key");
      return 1;
    }
    int n = strlen(argv[1]);
    int m = atoi(argv[1]);
    string cyphertext[n];
    string plaintext = get_string("plaintext: ");
    {
     printf("ciphertext: ");
     for (j = 0; j < strlen(plaintext) ; j++)
     
       {
       char t = plaintext[j];
       if (isalpha(t))
       {
        char x = 'A';
        if (islower(t))
        {
         x = 'a';
        }
        printf("%c", (t - x + m) % 26 + x);
       }
       else
       {
        printf("%c", t);
       }
        
      }
    }
      printf("\n");
   return 0;
}

bool key_check(string s)
{
  for (int i = 0, len = strlen(s); i < len; i++)
  if (isdigit(s[i]))
  {
  return true;
  }
  return false;
 }

I get the error as I mentioned when I use Check50 however when I try using non-numeric keys it works just fine. What is the issue here ?

0

It seems you are returning true when you find a digit in the string. What will your code return if string is '20x' or 'x12'?

In cases like this it is often advantageous to turn the problem upside down. So you want the string to be all digits, then look for non-digits, if you find one non-digit the whole string is invalid.

Source - Reddit Comment

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .