0

As title suggests I am really struggling with the logic in doing this...and its really making me think its not for me. But I WILL not give up!

The actual coding and telling the computer what to do and the logic behind argc and argv[] and strings being array of char and argv being a array of string and loops, conditions etc I get, its just solving the actual problem.

I am as far as checking for a command line argument (key) and then saving the key as int. Iterating through the plain text but for the life of me can't change the value in lets call it plaintext[i].

I was thinking along the lines of just changing the int value in plain[i] by key and then representing that as a char. I don't understand how it doesn't work, I just know it doesn't. Then I researched more and it says using alphabet index, and I get what it does, ish but then its implementing it into the problem.

Starting to think im not cut out for it. I do think its just the way I am trying to attack the problem, I have all the information I need to do it, I just cant see it. I have managed to do the previous problems sets, mostly on comfortable aswell. Normally takes me ages to think of a solution, trial and error but when I approach it right I fly through it.

I don't really know what im asking, any suggestions to think more like logical so I can approach the problem differently then how I am now?

Thanks edit Ok here is my code. This is as far as my logic goes, I will worry about keeping caps as caps and and lower and lower later when I can adjust the indexes.

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

int key;

int main(int argc, string argv[])
{
    if (argc == 2) //if argument in command line and its only 1 command
    {
        key = atoi(argv[1]); //atoi stores string as int
        string plain = get_string(); //get plaintext


        for(int i = 0; i < strlen(plain); i++) //for each char in plaintext
        {
            plain[i] = plain[i+key];
        }

    }
    else //if its 2 or more or less than 1
    {
        printf("Usage: ./caesar k\n");
        return 1;
    }
}

1 Answer 1

2
plain[i] = plain[i+key];

I can see what you're trying to do here and you're on the right track, but there's a problem. Think about what the value between the square bracket means.

1
  • Thanks for that helped alot:) I now realise plain[i] + key is changing the value of the index by key instead of changing the index. On to the next problem, how to make sure it only loops through A - z.... Commented Sep 29, 2017 at 10:03

You must log in to answer this question.

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