0

I am writing code for caesar but when i am executing the program i am not getting any output and when i try to print integer values instead characters it is giving weird integer values, with this i can conclude that i am getting values more than 127 but don't understand how that is happening in my code. can anyone please pint out the mistake.

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

char begin_index(char c );
char original_index(char c);


int main(int argc,string argv[])
{
    if (argc==2)
    {
        //printf("Success\n");
        //printf("Key : %s\n",argv[1]);
        for(int i = 0,n = strlen(argv[1]);i<n;i++)
        {
            if(isdigit(argv[1][i])==0)
            {
            printf("Usage: ./caesar key\n");
            //break;
            return 1;
            }
        }

        int key = atoi(argv[1]);
        printf("Key: %i\n",key);

        string plain_text = get_string("plain_text:");


        for (int i = 0,n = strlen(plain_text);i<n;i++)
        {
            if(isalpha(plain_text[i]))
            {
            plain_text[i] = begin_index(plain_text[i]);
            char ci = (plain_text[i] + key) % 26;

            ci = original_index(ci);

            printf("%c",ci);
            }

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






    else
    {
        printf("Usage: ./caesar key\n");
    }
}

char begin_index(char c )
{
    if(islower(c))
    {
        c = c - 97;

    }

    else if (isupper(c))
    {
        c = c - 65;
    }

    return c;

}

char original_index(char c)
{
     if(islower(c))
    {
        c = c + 97;

    }

    else if (isupper(c))
    {
        c = c + 65;
    }

    return c;
}
2
  • Can you show the output when you changed it into an integer values? – Ojou Nii Apr 26 '20 at 2:08
  • I think the problem was with using function ``` ci = original_index(ci)``` as in his case ci did not had proper ascii values so the function was not working properly – Bhavik Roopchandani Apr 26 '20 at 8:25
1

I suggest that you put a printf("%i",ci); instruction before you call your function here ci = original_index(ci); to check the value of your char variable 'ci'. With the value in mind, think what the output of the next function will be.

1

You must log in to answer this question.

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