0

I'm having trouble with the wraparound function of vigenere in (pset 2). The troublesome line of code is the one below, I suspect. Would greatly appreciate any and all advice! :)

int adjustedkey = (int) key[keycounter % strlen (key)];

complete code:

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

bool IsString(string x);
string Converttolower (string y);
int main (int argc, string argv[])

// Checks for only two arguments
{
    if (argc != 2)
    {
        printf ("Must contain only one argument!\n");
        return 1;
    }

// Checks that key is a pure string 

    if (!IsString (argv[1]))
    {
        printf ("Please key in a string with characters from A-Z or a-z only\n");
        return 1;
    }   

    if (argc == 2 && IsString(argv[1]))

    {
        string key = argv[1];
        string unencrypted = GetString();
        int keycounter = 0;

        for (int i = 0, n = strlen (unencrypted); i < n; i++)
        {
            // Defines variable adjustedkey such that it will incorporate dynamic keycounter and wrap around its max key length
            int adjustedkey = (int) key[keycounter % strlen (key)];

            if (isalpha(unencrypted[i]) && islower(unencrypted[i]))
            {
                if (islower(key[i]))
                {
                    int lower = ((int) unencrypted[i] + adjustedkey - 97);

                    if (lower > 122)
                    {
                        lower = (lower - 26);
                    }

                    printf("%c", (char) lower);
                    keycounter++;
                }

                else 
                {
                    int lower = ((int) unencrypted[i] + adjustedkey - 65);

                    if (lower > 122)
                    {
                        lower = (lower - 26);
                    }

                    printf("%c", (char) lower);
                    keycounter++;
                }
            }

            else if (isalpha(unencrypted[i]) && isupper(unencrypted[i]))
            {
                if (islower(key[i]))
                {

                    int upper = ((int) unencrypted[i] + adjustedkey - 97);

                    if (upper > 90)
                    {
                        upper = (upper - 26);
                    }

                    printf("%c", (char) upper);
                    keycounter++;

                }  

                else 
                {
                    int upper = ((int) unencrypted[i] + adjustedkey - 65);

                    if (upper > 90)
                    {
                        upper = (upper - 26);
                    }

                    printf("%c", (char) upper);
                    keycounter++;

                }
            }

            else
            {
                printf("%c", unencrypted[i]);
            }

        }

        printf("\n");
        return 0;
    }

}


//Checks whether command line input is pure string

bool IsString(string x)
{
 for (int i = 0, j = strlen(x); i < j; i++)
 {
     if (!isalpha (x[i]))
     {
         return false;
     }
 }
 return true;
}
0

I would write it in a simple way to get equivalent alphabet number regardless of if it is lower or capital.

int adjustedkey = (int)(key[keycounter]&0x1F) - 1;

I multiplied 0x1F to discard the 3 MSB (most significant bits) if you remeber first ASCII lecture. But now you will get 1 for B or b and 2 for c or C. But you have to remove ´97´ etc from your calculate of final letter

int lower = ((int) unencrypted[i] + adjustedkey- 97);

you just need int lower = ((int) unencrypted[i] + adjustedkey);

Note: you code still have problem with keycounter calculation. I will leave it upto you as it was not your question. good luck.

1
  • Thank you! Very helpful :)
    – Domoman
    Jul 10 '16 at 14:12

You must log in to answer this question.

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