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

int main(int argc, string argv[])

{
    if (argc != 2)
    {
        printf("Enter valid key!!\n");
        return 1;
    }

    string k = argv[1];
    int key = atoi(k);
    int a = 97;
    int A = 65;


    printf("enter the plain text:");
    string p = get_string();

    for(int i=0, n=strlen(p); i<n; i++)
    {
        if(isalpha ('p[i]'))
        {
            if(isupper ('p[i]'))
            {
                printf("%c", (((p[i]-A)+key)%26)+A);
            }
            else
            {
                printf("%c", (((p[i]-a)+key)%26)+a);
            }
        }
        else
        {
            printf("%c", p[i]);
        }
    }

printf("\n");


}
0
    if(isalpha ('p[i]'))

Placing single quotes around the variable p[i] means that its a literal string, not a variable. (Interestingly, it only throws a compile error if the string is two chars long. Also, single quotes normally indicate a char, not a string.) The isalpha() function takes a single char ONLY, so when this code tries to shove a string down it's throat, it chokes and throws up a segfault. If you want to test the char at p[i], remove the single quotes.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

0

the reason your program didn't get the right output is : a.in the functions isupper,isalpha you placed quotes around p[i].p[i] is a character itself so you need not keep quotes around it. b. also in if loop write give the information for printf as mentioned in problem statement.

You must log in to answer this question.

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