0

I am stuck at working out what is wrong with my code. I receive the values entered at the command line, I ask the user for text, I validate the command line values as characters of the alphabet and that they number 26 and are all different.

When I try to substitute the user entered text with the code characters entered at the command line I get the Segmentation error.

    string cypherText(string s, string t)
{
    string substitutionKey = t;
    string unencodedPhrase = s;
    string encodedPhrase = NULL;
    string alphabetLower = "abcdefghijklmnopqrstuvwxyz";
    string alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
    for(int i = 0; i < strlen(unencodedPhrase); i++)
    {
        for (int j = 0; j < 26 ; j++)
        {
            if (unencodedPhrase[i] == alphabetLower[j])
            {
                encodedPhrase[i] = substitutionKey[j];
            }
        }
    }
    return encodedPhrase;
}

The value passed in from the main section is the string that the user entered


string outPut = cypherText(inputPhrase, substitutionKey);

string substitutionKey = argv[i]; 

The substitutionKey is the string entered at the command line. The error checking to ensure the substitutionKey is all aphabetical and different values works.

The segmentation error occurs at this line

encodedPhrase[i] = substitutionKey[j]

The for loop is intended to loop through the user entered text to encode, starting at position 0, and find the character match in the command line entry and then put the relevant character form the command line entry into the new encoded phrase, and then move to the next character and so on until the end of the user entered text word sentence etc.

Any advice is appreciated as to the remedy.

1 Answer 1

1
encodedPhrase[i] = substitutionKey[j];

This code above is causing error because encodedPhrase[i] is trying to access memory space which is not allotted to it. Your code has even several logical errors such as:

  • It cannot detect upper case letters in user input.
  • It cannot detect for special characters or whitespace.

You can use the below code for reference

    string unencodedPhrase = get_string("plaintext:"); //declaring string to accept user input
    int len = strlen(unencodedPhrase);
    printf("\n");
    int store; // this variable helps to find relative character in (arg[1]) or substitutionKey

    for (int a = 0; a < len; a++)
    {
        if (islower(unencodedPhrase[a])) //making condition statements to change to cipher
        {
            store = unencodedPhrase[a] - 'a';
            unencodedPhrase[a] = tolower(argv[1][store]);
        }
        else if (isupper(unencodedPhrase[a])) //making condition statements to change to cipher
        {
            store = unencodedPhrase[a] - 'A';
            unencodedPhrase[a] = toupper(argv[1][store]);
        }
    }
    printf("ciphertext: %s", unencodedPhrase); //printing the cipher text for the user
    printf("\n")
2
  • Many Thanks,I knew where the problem was, just not how to fix it.
    – Mike1965SA
    Nov 7, 2020 at 6:10
  • Welcome, It would be nice if you would upvote the answer for future references. Nov 7, 2020 at 9:16

You must log in to answer this question.

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