Skip to main content
deleted 21 characters in body
Source Link

Having such a hard time getting the key to cycle back after first iteration, any hint would be great as i've been stuck on this for a long time. don't mind the constant printf at the beginningit does baconbbaconbbacon.

Having such a hard time getting the key to cycle back after first iteration, any hint would be great as i've been stuck on this for a long time. don't mind the constant printf at the beginning.

Having such a hard time getting the key to cycle back after first iteration, any hint would be great as i've been stuck on this for a long time. it does baconbbaconbbacon.

Source Link

Help with Vignere pset2

Having such a hard time getting the key to cycle back after first iteration, any hint would be great as i've been stuck on this for a long time. don't mind the constant printf at the beginning.

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

int main(int argc, string argv[])
{
    int i = 0;
    string s = argv[1];
    //recieve first argument
    if (argc != 2)
    {
        printf("Key not provided, try again.\n");
        return 1;
    }
    //check to see if argv[1] is alphabetical
    if (isalpha(s[i]))
    {
        for (int n = strlen(s); i < n; i++)
        {
            printf("Current int: %c converted to: %i\n", s[i], s[i]);
        }
        printf("Alphabet check passed...Proceeding...\n");
        printf("%s\n", s);
    }
    else
    {
        //non-alphabetical key entered
        printf("Key is not alphabetical\n");
        return 1;
    }
    //ask for plaintext
    printf("plaintext: ");
    string plain = get_string();
    //ciphertext
    printf("ciphertext: ");
    s = argv[1];
    i = 0;
    int conv = s[i];
    if (s != NULL)
    {
        for (int k = 0, x = strlen(plain), key = 0, n = strlen(s); k < x; k++, key++)
        {
            if (i == 0)
            {
                conv = s[i];
                i++;
            }
            else if (i < n)
            {
                conv = s[i];
                i++;
            }
            else if (i >= n)
            {
                i = (i - i);
                conv = s[i];
            }
            if (conv >= 97)
            {
                conv = (conv - 97);
            }
            else if (conv <= 90 && conv >= 65)
            {
                conv = (conv - 65);
            }
            if (plain[k] == ' ')
            {
                k++;
                printf(" ");
            }
            if (isupper(plain[k]))
            {
                if (key >= n)
                {
                    key = (key - n);
                }
                printf("%c", (( plain[k] - 'A' + conv) % 26) + 'A');
            }
            else if (islower(plain[k]))
            {
                if (key >= n)
                {
                    key = (key - n);
                }
                printf("%c", (( plain[k] - 'a' + conv) % 26) + 'a');
            }
         }
    }
    printf("\n");
}