Ad

Change Letters According To A Key (Encryption)

- 1 answer

I'm trying to encrypt a input from the user (variable text) by a cyphertext with a given key (variable argv[1]). Where A is replaced by the first letter of the key, B is replaced by the second letter of the key etc.

For example if I input in the terminal: ~./test YTNSHKVEFXRBAUQZCLWDMIPGJO

And the user types in the following text text: OVERFLOW

The output is: ciphertext: QIHLKBQP

My problem is now that my function does not work for the following user input: The quick brown fox jumps over the lazy dog with the key ./test DWUSXNPQKEGCZFJBTLYROHIAVM

What I get: ciphertext: Rqx tokug wljif nja eozby jhxl rqx c

What I want to get: ciphertext: Rqx tokug wljif nja eozby jhxl rqx cdmv sjp

Why does my code stop replacing the letters after the reaching the c and stops adding "... cdmw sjp"?

My current code is:

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



int l = 97;
int j = 65;
int difference;
int differencee;
int k=0,b, c;



int main(int argc, string argv[])
{
    string text = get_string("Plaintext: ");     // Asking for the text-input


    printf("ciphertext: ");
    for(i = 0; i < strlen(text); i++, j++, l++) //loop for the given text length
    {
        difference = argv[1][i] - j;
        differencee = argv[1][i] - l;

            if(j==text[k] && text[k]<91 && text[k]>64) // Check for UPPERCASE LETTERS and change according to key
            {
                b = text[k] + difference;

                k++;
                printf("%c",toupper(b));
                i = 0;
                j = 64 +1;
                l = 96 + 1;
            }
            if(l==text[k] && text[k]<123 && text[k]>96)   //Check for LOWERCASE LETTERS and change according to key
            {
                c = text[k] + differencee;

                k++;
                printf("%c",tolower(c));
                i = 0;
                l = 96 + 1;
                j = 64 + 1;
            }
            if(isalpha(text[k]) == 0 && text[k] != '\0') //If not alphabet, then do nothing
            {
                printf("%c",text[k]);
                k++;
                i = 0;
                
            }
    }
    printf("\n");
}
Ad

Answer

You can solve your algorithm simply. No need to extra computation and variables.

Check this for your example and change it to cover your work: Input and output is as your desired.

#include <math.h>
#include <ctype.h>
#include <string.h>

int main()
{
    char text[64] = "The quick brown fox jumps over the lazy dog\0";     // Asking for the text-input

    char argv[32] = "DWUSXNPQKEGCZFJBTLYROHIAVM";
    int len = strlen(argv);
    int i=0;
    char temp = 0;
    int index = 0;
    printf("strlen(text): %d strlen(argv) = %d\n ",strlen(text),len);
    printf("ciphertext: ");
    for(i = 0; i < strlen(text); i++) //loop for the given text length
    {   
        if(isalpha(text[i]) == 0 && text[i] != '\0') //If not alphabet, then do nothing
        {
            printf("%c",text[i]);
            continue;
        }
            
        temp = text[i];
        
        if(temp >= 'A' && temp <= 'Z') // Check for UPPERCASE LETTERS and change according to key
        {
            temp +=32;
        }
            
        if(temp >= 'a' && temp <= 'z')   //Check for LOWERCASE LETTERS and change according to key
        {
            index = (temp - 'a')%len;
            printf("%c",argv[index]);
        }
            
    }
    printf("\n");
}

EDITTED: Online Demo

Ad
source: stackoverflow.com
Ad