0

when running the code below with a command line ./caesartest 2, I get the false return 1, not the true, what is wrong? (I began having the trouble after including isdigit)...

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


int main(int argc, string argv[])


{

        int k = atoi(argv[1]);

        if (isdigit('k'))

                {

                string s = GetString();

                for (int i=0, n= strlen(s); i<n; i++)
                    {
                        if (isalpha('s[i]'))
                            {
                            char l = s[i] - 65;
                            char m = (l + k) % 26;
                            char p = (m + 65);

                            printf("%c", p);
                            }
                        else
                            {
                                printf("%c", s[i]);
                            }
                    } 
                    printf("\n");
                }

        else
        {
            printf("1\n");
            return 1;
        }

   return 0;
   }
1
if (isdigit('k'))

'k' refers to the character or letter k, note that atoi converts the character of the command line to an integer, surely you want to do

if (isdigit(argv[1])

however this is not correct because argv [1] is a string, not a character, the best since my understanding is not used isdigit, is a pset too early to get into trouble with strings.I think you can use: if (isdigit(argv[1][0])), but only is used for a character, for various characters you need a loop

if (isalpha('s[i]')

is another mistake, s[i] it is a character (corresponds to ith character of the string s)

You must log in to answer this question.

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