I keep getting a seg fault when I run vigenere with a keyword, I think I may be using the isalpha() function incorrectly, please can someone take a look and give me some pointers pls? Below are a couple of lines of code to help get to the root of the problem:

int key = atoi(argv[1]);
char letr[key];

for (int c = 0, p = strlen(plaintext); c < p; c++)
for(int j = 0, k = strlen(argv[1]); j < k; j++)
    if (isupper(letr[c]))
    printf("%c", (plaintext[key] - 65 + key) % 26 + 65);

Pls also let me know if you spot any potential pitfalls with my formula.

Many thanks


The reason my seg fault still came up after making changes following @DinoCoderSaurus' advice regarding the atoi function was that I hadn't converted the string input to char when using isalpha to ensure the keyword entered only contained alphabetical characters.

To correct this, I used a for loop to iterate over and check each character. It was a Duh! moment

Thank you for your help @DinoCoderSaurus'


Actually, it's the atoi function here int key = atoi(argv[1]);. Since vigenere takes a "word" as the argument, it cannot convert it to an "integer".

From man atoi:

.... atoi() does not detect errors.

So atoi(argv[1]) is probably returning 0, so char letr[key]; is creating a "0 length" array. It's downhill from there.

You are on the right track. Think about using "ascii arithmetic" to transform the characters in argv[1] into what you want.

Before you get too far into this, you will (almost) always run into problems if you attempt vigenere with nested loops. Remember from the spec "Your program must only apply Vigenère’s cipher to a character in p if that character is a letter. ". So there is "skipping" involved. Basically you need to process each letter in the plaintext exactly once. If you have an inner loop that processes strlen(key) times, it will process each letter in the plaintext strlen(key) times.

  • Hi, thank you for your help so far, I have changed my code to this: – Sofia Jan 26 '17 at 18:36
  • Sorry pls ignore above message, as I was pasting the code in, I think I realised what the problem is and am going to give it another crack! – Sofia Jan 26 '17 at 18:38
  • have made several changes but am still getting a seg fault, again not sure where I'm going wrong, just guessing and making changes to code now! Can I have some more direction pls, here is updated code: – Sofia Jan 26 '17 at 20:11
  • Your code never got pasted, you must have gotten past the seg fault since you posted another question. Good on ya. You should add an answer to this question explaining how and what you fixed, so that it might help others in the same predicament. Then accept it as the answer (though you need to wait 2 days to accept your own answer). – DinoCoderSaurus Jan 26 '17 at 21:48

You must log in to answer this question.

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