0

so this is my code. All problems are compiled successfully except this one. The output is "Segmentation fault"

this is my code:

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

int main (int argc, string argv[])
{
string key1 = argv[1];

//check for only 2 arguments//
if (argc != 2)
{
    printf("Usage: ./substitution key\n");
    return 1;
}

//check key length, must 26 lenght//
if (strlen(key1) != 26)
{
    printf("Key must contain 26 characters.\n");
    return 1;
}

for (int i=0; i < strlen(key1); i++)
{
    //check for non alphabet//
    if (!(isalpha(key1[i])))
    {
        printf("Key must contain only letters.\n");
        return 1;
    }

    //convert all word to uppercase to make encryption easier//
    else
    {
        key1[i] = toupper (key1[i]);
    }

    //check for not repeated word//
    for (int j=0; j < i; j++)
    {
        if (key1[i] == key1[j])
        {
            printf("Key must not contain repeated letters.\n");
            return 1;
        }
    }
}

//ask input//
string input1 = get_string("plaintext: ");
string input2 = input1;

//check if the word is lower or uppercase//
int checker[25];
for (int s = 0; s < strlen (input1); s++)
{
    if (isupper(input1[s]) == 0)
    {
        checker[s] = 0; //false

        //convert to uppercase to make encryption easier//
        input1[s] = toupper (input1[s]);
    }
    else
    {
        checker[s] = 1; //true
    }

    //printf("Input %d: %c is %d\n", s, input1[s], checker[s]); //debug//

}

//printf("word is %s\n", input1); //debug//

//CONVERTTTT//
int new_ascii = 0;
printf("ciphertext: ");

for (int k = 0; k < strlen(input1); k++)
{
    char x = input1[k];
    int ascii = (int) x;

    //check if it's alphabet or not//
    if (isalpha(x))
    {
        ascii -=  65;
        new_ascii = key1[ascii];

        //restore to upper/lowercase
        if (checker[k]==0)
        {
           new_ascii = (int) tolower ((char) new_ascii); //we use tolower cause all word has converted to upper
        }

    }
    else //if not just continue
    {
        new_ascii = ascii;
    }

    printf("%c", (char) new_ascii);
}

printf("\n");

return 0;
}

and this is the output:

./substitution DWUSXNPQKEGCZFJBTLYROHIAVM
plaintext: The quick brown fox jumps over the lazy dog
Segmentation fault

I don't know where's my problem since this is the only one can't be compiled. Does anyone have clue?

1
  • First of all, if you're running other tests, then the program is compiled. Compile and execute are two very different things. A seg fault is a run-time error, meaning that it has successfully compiled, but failed in execution. You need to figure out where the seg fault occurred (which line) and analyze it. Have you run it with valgrind? It'll tell you which line.
    – Cliff B
    Sep 17 at 8:34
0

After looking at the code, it might still be difficult to figure out where the problem lies, because the root cause lies elsewhere. This is a good example of how a subtle oversight can cause a major problem.

It appears that the code uses the checker[] array to track whether a letter is upper or lower case. Here's the declaration:

//check if the word is lower or uppercase//
int checker[25];

The size of checker has been set to an arbitrary size of 25. The test that fails uses a long sentence, longer than 25 characters. So, when the code executes, it tries to access beyond the end of the array. That's causing a seg fault at runtime.

The program is supposed to accept any length of text. What if the input were a paragraph? A short story?? War and Peace???? What arbitrary value are you going to use?

In a situation like this, you need to process each character, one by one, preserving case as you process each one. There's no need to have an array for that. You just need to preserve the case as you go. The code can reuse a simple variable over and over for each character.

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

1

You must log in to answer this question.

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