0

QUESTION

  1. When i put cipheralgorithm (which is a function that is making encryption) in comment section (/*) everything goes perfect i can get input when i type ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO or something else like that which dont have same char values as PSET2 instruction says.

  2. But when i open comment section in cipheralgorithm 'P' char gives error. Program started to think that there was 'P' character use before

  3. After i open command line in cipheralgorithm and put 'P' char at the end of the command line argument like ./substitution YTNSHKVEFXRBAUQZCLWDMIGJOP it works fine again

  4. WHERE IS THAT 'P' ? :)

Here is the code

string cipheralgorithm(string plaininput, char input[]);

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

    if(argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }

    char inputLettersAsciiValues[26];

    for (int i = 0; i < 26; i++)
    {
        if ((int)argv[1][i] < 65 | (int)argv[1][i] >= 91 && (int)argv[1][i] <= 96 | (int)argv[1][i] > 122)
        {
            printf("Wrong key\n");
            return 1;
        }
        else
        {
            if ((int)argv[1][i] < 91)  // Between A-Z
            {
                int changedAsciiValuesToLower = (int)argv[1][i] + 32;

                for (int j = 0; j < 26; j++)
                {
                    if (inputLettersAsciiValues[j] == changedAsciiValuesToLower)
                    {
                        printf("Wrong Key/ sameupper %c characters \n", inputLettersAsciiValues[j]);
                        return 1;
                    }
                }

                inputLettersAsciiValues[i] = changedAsciiValuesToLower;
                //printf("%c",inputLettersAsciiValues[i]);
            }
            else
            {
                for(int k = 0; k < 26; k++)
                {
                    if(inputLettersAsciiValues[k] == (int)argv[1][i])
                    {
                        printf("Wrong Key/ same %c characters \n",inputLettersAsciiValues[k]);
                        return 1;

                    }
                }

                inputLettersAsciiValues[i] = (int)argv[1][i];
            }
        }
    }
    string plaintext = get_string("plaintext: ");
    printf("ciphertext: %s", cipheralgorithm(plaintext, inputLettersAsciiValues));
}


string cipheralgorithm(string plaininput, char input[])
{

    char lowerAsciiLetterForKey[26];
    char upperAsciiLetterForKey[26];

    char lowerLetterAlphabet[26];
    char upperLetterAlphabet[26];

    //CREATING KEY LETTERS FOR MATCHING WITH ALPHABET

    for (int x = 0; x < 26; x++)   //we take all lower case letters ordered in a char[];
    {
        lowerAsciiLetterForKey[x] = input[x];
    }
    for (int y = 0; y < 26; y++)   // we create all upper case letters form key letters
    {
        upperAsciiLetterForKey[y] = lowerAsciiLetterForKey[y] + 32;
    }

    //CREATING ALPHABETS FOR LOWER AND UPPER LETTERS

    for (int a = 65; a < 91; a++)  //A -Z
    {
        upperLetterAlphabet[a-65] = a;
    }
    for (int b = 97; b < 123; b++) //a -z
    {
        lowerLetterAlphabet[b-97] = b;
    }

    int plaintextCharCount = strlen(plaininput);

    char plaininputAsciiValues[plaintextCharCount];
    char cipheroutputAsciiValue[plaintextCharCount];


    for (int i = 0; i < plaintextCharCount; i++)
    {
        plaininputAsciiValues[i] = (int)plaininput[i];
    }


    for (int k = 0; k < plaintextCharCount; k++)
    {
        if (plaininputAsciiValues[k] >= 97 & plaininputAsciiValues[k] <= 122)
        {
            for (int d = 0; d < 26; d++)
            {
                if (plaininputAsciiValues[k] == lowerLetterAlphabet[d])
                {
                    cipheroutputAsciiValue[k] = lowerAsciiLetterForKey[d];
                }
            }
        }
        else if(plaininputAsciiValues[k] >= 65 & plaininputAsciiValues[k] <= 90 )
        {
            for (int f = 0; f < 26; f++)
            {
                if  (plaininputAsciiValues[k] == upperLetterAlphabet[f])
                {
                    cipheroutputAsciiValue[k] = upperAsciiLetterForKey[f];
                }
            }
        }
        else  //diger degerler gelmisse . / ,  vs
        {
            cipheroutputAsciiValue[k] = plaininputAsciiValues[k];
        }
    }

    char * output = malloc(plaintextCharCount + 1);
    for (int o = 0; o < plaintextCharCount; o++)
    {
        output[o] += cipheroutputAsciiValue[o];
    }
    output[plaintextCharCount + 1] = '\0';

    return output;
}

0

I solved the problem changing these lines in cipheralgorithm

 for (int y = 0; y < 26; y++)   // we create all upper case letters form key letters
    {
        upperAsciiLetterForKey[y] = lowerAsciiLetterForKey[y] + 32;
    }

By changing these lines

for (int y = 0; y < 26; y++)   // we create all upper case letters form key letters
    {
        upperAsciiLetterForKey[y] =  toupper(lowerAsciiLetterForKey[y]) ;
    }

Probably toupper() function adding a better 32 than me! Exactly acts same but result is different anyways.

You must log in to answer this question.

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