Home > other >  Trying to write a loop to replace letters in a string with letters from a different string at the sa
Trying to write a loop to replace letters in a string with letters from a different string at the sa

Time:11-24

I'm working on a program that is supposed to take a key as an input argument, and encrypt a user input word using this key.

The program should:

  1. Ask the user for a plaintext word to encrypt

  2. Standardize the letter case

  3. Take each letter from the plaintext and find the index of this letter (A = 0, B = 1,...)

  4. Look at the letter indexed at this location in the key string (input argument)

  5. Assign this encrypted letter to a new sting called cypher

  6. Print the new cyphertext string.

The code I'm using is this:

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

int main(int argc, string argv[])
{

    //Check that key has 26 letters or end program
    string key = argv[1];
    if (strlen(argv[1]) != 26)
    {
        printf("Key must contain 26 characters\n");
        return 1;
    }

    //Get plaintext
    string plain = get_string("plaintext: ");

    //Make key all letters upper case
    for (int i = 0; i < plain[i]; i  )
    {
        if (islower(plain[i]))
        {
            plain[i] = plain[i] - 32;
        }
        printf("%c", plain[i]);
    }
    printf("\n");

//Encrypt
    int index[] = {};
    int cypher[] = {};

    //Cycle through the letters in the word to be encoded
    //printf("cyphertext: ");
    printf("%c\n", key[79 - 65]);
    for (int i = 0; i < strlen(plain); i  )
    {
        printf("index in key: %i\n", plain[i] - 65);
        cypher[i] = key[plain[i] - 65];
        printf("cypher: %c\n", cypher[i]);
    }
    printf("\n");
}

Everything executes fine until the fourth loop of the for loop that assigns the new values to the cypher string. When the program tries to set i = 4, I get the error Segmentation fault (core dumped)

I was expecting the last for loop to loop once for each letter of the input (e.g. input: hello; loops: 5), but I found that it stops at 4 and only outputs: 'HELL'.

I tried:

  • Words with 4 characters - executes the correct number of loops, but I still get Segmentation fault (core dumped) after the final loop

  • Words with 3 characters - executes fine, no error

  • Words with 5 letters - Still loops 4 times before error

Please help!

CodePudding user response:

The for loop should iterate from 0 to length of plain.

//Get plaintext

   string plain = get_string("plaintext: ");

    //Make key all letters upper case
    for (int i = 0; i < strlen(plain); i  )
    {
        if (islower(plain[i]))
        {
            plain[i] = plain[i] - 32;
        }
        printf("%c", plain[i]);
    }

//*** Must allocate memory for array
//Encrypt
    int index[100] = {};
    int cypher[100] = {};

    //Cycle through the letters in the word to be encoded
    //printf("cyphertext: ");
    printf("%c\n", key[79 - 65]);
    for (int i = 0; i < strlen(plain); i  )
    {
        printf("index in key: %i\n", plain[i] - 65);
        cypher[i] = key[plain[i] - 65];
        printf("cypher: %c\n", cypher[i]);
    }
    printf("\n");
  • Related