So i wrote my program, but when i start it, i get the segmentation fault, yet when i start it with debug50 it works, i know what is the segmentation fault, but i can't figure out why and where would this segmentation fault happen,

I was thinking that it might happen in line 33 if the condition to reset the key letter doesn't trigger (if i reach the last letter of the key, go back to the first letter), but there are no reasons that it wouldn't

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

// Constants

// Functions prototypes
int charCrypter(char keyChar, char uncryptedChar);
int indexValue(char asciiChar);

int main(int argc, string argv[])
    // Exit program if CLI is wrong
    if (argc != 2 || isalpha(argv[1]) == false)
        return 1;

    string userKey = argv[1];
    string userInputText = get_string("Text: ");
    int letter = 0;

    // Main loop of the program
    for (int i = 0, n = strlen(userInputText); i < n; i++)
        if (isalpha(userInputText[i]))
            // Loop the key
            if (letter == strlen(userKey))
                letter = 0;
            printf("%c", charCrypter(userKey[letter], userInputText[i]));
            printf("%c", userInputText[i]);

// Encrypt the character
int charCrypter(char keyChar, char uncryptedChar)
    // get the index value
    int keyIndex = indexValue(keyChar);
    int userIndex = indexValue(uncryptedChar);

    int alphabeticalValue = (keyIndex + userIndex) % ALPHABETICALLENGTH

    // Cipher the uncrypted char
    if (islower(uncryptedChar))
        return alphabeticalValue + ASCIILOWERCASE;
    return alphabeticalValue + ASCIIUPPERCASE;

// Get index value
int indexValue(char asciiChar)
    if (islower(asciiChar))
        return asciiChar - ASCIILOWERCASE;
    return asciiChar - ASCIIUPPERCASE;

Your segmentation fault is happening at this line:

    // Exit program if CLI is wrong
    if (argc != 2 || isalpha(argv[1]) == false)

You might take a look at the documentation of isalpha. The function isalpha takes a char as its argument. However, you give it a string as argument ...

Hint on how to solve this: Remember that a string is basically a char * or a char[] ...

Finally, to quote Cliff B:

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

| improve this answer | |
  • Thank you very much for your answer :) What would be the best way to resolve this problem to return 1 if a character of argv[1] is not an alpha ? i didn't see char * in my lessons yet, i think it means pointer or something but we didn't see it yet, but i saw that a string is an array of char, is my best way to resolve this problem by implementing a for loop ? with an if condition to see if a character is not alpha for (int i = 0, o = strlen(argv[1]); i < o; i++) { if (isalpha(argv[1][i]) == false) { return 1; } } are there any better ways ? – Arjun Singh Oct 24 '18 at 8:13
  • In this case, the specifications say (amongst others) that you have to return 1 if the (single) argument contains a non-alpha character. That means that you'll have to check all characters, so you'll probably have to use a loop for that. So you should start by splitting up the input checks into one which checks the number of argument, and one which checks for non-alpha's. ... – Peter Pesch Oct 24 '18 at 8:20
  • The loop you mention looks OK to me. – Peter Pesch Oct 24 '18 at 8:22

You must log in to answer this question.

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