0

I can't figure out what to do with Segmentation fault (core dumped) in Vigenere. I tried loops - not working. Could you please help!!!

Thank you in advance!

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

int main(int argc, char *argv[])
{
char key[25];
char text[300];
int i;

//ask for user input;

printf("Please enter your text. It will be encrypted by vigenere encrypted method\n");
scanf("%s",text);

//ask user to input the key
printf("Please enter any word up to 10 letter long. We will use it as a key to encrypt your text\n");
scanf("%c", key);

// wrap around the key
for (i=0; i< 300 && key[i]!=0; i++ )

//Verify that input accept letters only 
    if(!isalpha(key))

    {
        printf("You entered non alphabitic character. Please retry and enter lower case letters only]n");

    }


//Verify only 1 key and 1 sentence should be provided    
        if (argc!= 2)
    {
        printf("Please provide only one sentence to encrypt and only one word for key\n");
        return 1;
    }
//Text encryption       

          for (int i = 0; i < 300 && text[i] != 0; i++)
             {
                 if (text[i]>='a' && text[i]<='z')
                 text[i] = (text[i] -'a' + key[i])%26 +'a';

                 else if(text[i]>='A' && text[i]<='Z')
                 text[i]= (text[i] - 'A' + key[i])%26 +'A';
             }
//Printing the encrypted text
printf("Here is your encrypted text: %s\n", text);
return 0;
}
1
  • Just would like to say Thanks to Cliff B , who answered my question! – Marina Aug 13 '15 at 16:30
0

Your seg fault is here: if(!isalpha(key)). isalpha takes a single character as an argument. You are trying to feed an entire string into it, and that doesn't work. If you want to check every character in the string, you need to do it one at a time, using a for loop.

You have other issues as well. Most of them revolve around a misunderstanding of the assignment and of the use and definitions of argc and argv[]. For example, the program requires the key to be entered on the command line when you start the program, not to be requested as input by the program. I recommend that you review the assignment in detail and the class materials and shorts covering argc and argv arguments. CHECK50 expects very exact inputs and outputs, including the correct spaces, line feeds, inputs, prompts and outputs. If you don't match the assignment expectations exactly, check50 will not accept the result. In cases where there is a staff solution available to run, you should always run theirs to get a sample output to compare.

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

0

isalpha() returns a number > 0 if there is an alphabetic character in the parentheses, and 0 if not.

Also, "key" is a string and isalpha() only accepts characters.

You must log in to answer this question.

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