0

I am focusing on lower case only until I get it working, any help??

if(argc != 2) // if not two line commands prints error and stops program
{
    printf("Please enter a single key");
    return 1; 
}


{
    if(argc == 2)// checks to see if it has only two line commands
        ;
    for(k = 0, n = strlen(argv[1]); k < n; k++)

{
    if (isdigit(argv[1][k]) )

    {

        printf("error");
        return 1;
    }


    string key = argv[1];//command line keyword to help encrypt

    int keylengh = strlen(key);

    printf("\n" "Enter your text to be converted""\n");

    string text = GetString();// request the text to be converted

    for (i = 0, k=0, n = strlen(text); i < n; i++,k++)


    {
        if (islower(text[i]))
        { 
            // shifts the lower case text to be printed by the amount of the key 
            printf("%c", ((text[i]-'a' ) + ((key[k%keylengh])-'a')%26) + 'a');
        }


        else 
        {
            printf("sorry");
        } 
    }

}
1
  • Please indent your code
    – sinister
    Nov 16, 2014 at 2:22

1 Answer 1

1

A few errors to point out:

  1. your code is difficult to read because it has not been indented properly.

  2. I believe your formula for shifting the characters is wrong. Lets say we take 'z' which is 122 on the ascii table. subtract 'a', which is 97, and add a arbitrary key of maybe 13, followed by 'a' again. This gives a result of 135.

  3. you should avoid placing your formula in printf. It would be easier and neater to declare a new variable to work with then print this new variable after you have applied the cipher.

Some tips:

  1. Remember that users can also key in different case characters for the key and both 'A' and 'a' has to be taken as the same. Perhaps you can declare a variable to store the key and then convert it to the cipher in terms of numbers first? Then you can just apply these numbers to the user's uncoded message later on.

  2. Think about using modulo to allow both the key and the message wrap around the alphabet. Recall that a simple application of your formula gave me 135 instead of wrapping back start from 'a' again? Understanding how modulo works will be useful.

You must log in to answer this question.

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