Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CS50 Pset2, Caesar, handles non-numeric key problem

When I use the function I created, only digits, and call it in main, it doesn’t handle non-numeric keys. But when I type the body of the function only_digits right into main without calling it, it works just fine.

The specific issue is

:(( handles non-numeric key

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

running ./caesar 2x

cause: timed out while waiting for program to exit

Below is all the code, but instead of calling only digits in main(bool digits = only_digits(argv);), I just wrote the body of it,

for (int i = 0; i < strlen(argv[1]); i++)
        {
            if (isdigit(argv[1][i]) == false)
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }

in main to get all the checks to work for cs50.

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

bool only_digits(string argv[]);

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        //run loop to check if each caracter in string, argv[1], is an integer 0-9, if it isn't then quite the program(return 1)
        for (int i = 0; i < strlen(argv[1]); i++)
        {
            if (isdigit(argv[1][i]) == false)
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }

        //convert the string key into integer key
        int k = atoi(argv[1]);
        //prompt user for plaintext to encrypt
        string plaintext = get_string("plaintext:  ");
        printf("ciphertext: ");

        //run loop to to the caesar calculation
        for (int i = 0, n = strlen(plaintext); i < n; i++)
        {
            //check if each character in plaintext the user inputted is between 'a'-'z', or 97-122
            if (plaintext[i] >= 'a' && plaintext[i] <= 'z')
            {
                printf("%c", (((plaintext[i] - 'a') + k) % 26) + 'a');
            }
            //check if each character in plaintext the user inputted is between 'A'-'Z', or 65-90
            else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')
            {
                printf("%c", (((plaintext[i] - 'A') + k) % 26) + 'A');
            }
            //If character isn't alphabet, just print what was inputted back to user
            else
            {
                printf("%c", plaintext[i]);
            }
        }
        printf("\n");
        return 0;

    }
    else
    {
        //Print error showing how to use the program and type a command-line argument
        printf("Usage: ./caesar key\n");
        return 1;
    }
}

bool only_digits(string argv[])
{
    for (int i = 0; i < strlen(argv[1]); i++)
    {
        if (isdigit(argv[1][i]) == false)
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
    return argv;
}

The problem says I have to add a function called only_digits which is why I can’t just submit it as is.

>Solution :

This statement

return argv;

does not make sense. The compiler can issue a message for this statement (though it is not necessary). The return type of the function is bool but you are returning an expression of the type string * that never will be converted to false.

The function can be defined the following way

bool only_digits( string key )
{
    if ( *key == '\0' ) return false;

    while ( *key && isdigit( ( unsigned char )*key ) ) ++key;

    return *key == '\0';
}

The function return true if all characters represent digits otherwise false.

And the function can be called like

if ( !only_digits( argv[1] ) )
{
    puts( "Usage: ./caesar key" );
    return 1;
}

If you do not know yet what this expression *key means then the function can be defined the following way

bool only_digits( string key )
{
    size_t i = 0;

    while ( key[i] != '\0' && isdigit( ( unsigned char )key[i] ) ) ++i;

    return i != 0 && key[i] == '\0';
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading