0

It is because I am trying to access memory that I shouldn't be, correct? Can anyone tell me where my error is?

   //prompt the user for a non-neg integer 'k' which is to be used as
//an arg

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

int main (int argc, char *argv[])
{
    if (argc!=2)                //fail if 0 or more than one key
    {
        printf ("PROVIDE JUST ONE COMMAND LINE ARGUMENT\n");
        return 1;
    }
    if (isalpha(argv[1])==0)     //fail if key is not a word
    {
        printf ("KEY MUST BE A WORD\n");
    }
    string p = GetString();     //retrieve phrase to be encrypted
    int keylength=strlen(argv[1]);   // get length of keyword
    int plength = strlen(p);        // get length of phrase
    int *newkey=((int*)malloc(sizeof(keylength)));
    //deal with upper and lower case chars in keyword first
    for (int j=0; j<keylength; j++) //iterate through each char in key
    {
        if (islower(p[j])!=0)   //if key char is lower case...
        {
            char x = (p[j]-97);
            int y = atoi(&x);
            newkey[j]= y;  
        }
        if (isupper(p[j])!=0)   //if key char is upper case...
        {
            char x = (p[j]-65);
            int y=atoi(&x);
            newkey[j]=y;
        }
    }
    // now will iterate through each char in the phrase...
    for (int i=0, h=0; i<plength; i++, h++)
    {
        char c = p[i];              //if char is not a letter...
        if (!isalpha(c))
        {
            printf ("%c", c);
            h=(h-1);
        }
        else if (isupper(c)!=0)      //if char is uppercase...
        {   
            c = ((c-65+newkey[h])%26)+65;
            printf ("%c", c);
        }
        else if (islower(c)!=0)     //if char is lowercase...
        {
            c = ((c-97+newkey[h])%26)+97;
            printf ("%c", c);
        }
        if (h==keylength-1)
            h=0;
    }
    free (newkey);
    printf ("\n");
}
1
if (argc!=2 || isalpha(argv[1])==0)

isalpha() takes a single character as input. You're trying to stuff it with an entire string. Instant seg fault.

Note that when argc!=2 is true, then the isalpha isn't evaluated or executed because as soon as any part of an or statement is found to be true (they're generally evaluated left to right), the remaining clause(s) are ignored.

You'll also do better to split the if test into two separate code blocks- one to test for the correct number of arguments and the other to validate that the key is all alpha.

That's why you have a seg fault. If there are other issues, well, that's another question. ;-)

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

1
  • BTW, the "else" after the first if statement is redundant. If the first if is true, you're going to exit no matter what, and if false, you'll execute the remaining code no matter what. No need to complicate the code by wrapping it in an else and a set of curly braces. – Cliff B Oct 4 '15 at 1:54

You must log in to answer this question.

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