0

I am getting a segmentation fault error for my code for Caesar, but I do not know what this means or know how to fix it.

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

int main(int argc, string argv[])
{
    if (argc!=2)
    {
        printf("Error: Incorrect number of arguments specified.\n");
        return 1;
    }

    string key = argv[1];
    int k = atoi(key);

    printf("plaintext: ");
    string s = get_string();
    printf("ciphertext: ");
    for(int i=0, n = strlen(s); i<n; i++)
    {
        if (isalpha('s[i]'))
        { 
            if (isupper ('s[i]'))
            {
                 int j = s[i] - '0';
                 j-=65;
                 int c = (j+k)%26;
                 c+=65;
                 printf("%c", (char) c);
            }
            else
            {
                int j = s[i] - '0';
                j-=97;
                int c = (j+k)%26;
                c+=97;
                printf("%c", (char) c);
            }
        }  
        else
        {
            printf("%c", s[i]);
        }
     }
     printf("\n");

}
0

In C, anything between single quotes (ticks or ') is interpreted as a char. Anything between double quotes (") is interpreted as a string.

These lines if (isalpha('s[i]')) and if (isupper ('s[i]')) are a problem because of the ticks. It is essentially trying to read 4 characters (s[i]) into a char.

You want to test the contents of s[i], which is written that way.

You must log in to answer this question.

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