Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

Pset2 Vigenere help

0 votes
asked May 27 in Pset2 by James-_- (120 points)

Ok I have probably made this way more complicated than it needs to be...

This is what I have so far and I have been stuck for a while now.

When I run it with the key of: AaBbCcXxYyZz

This is what I get back:
Plaintext: ABCabcABCabcABCabc
Ciphertext: AbDbDeXy[y[|~/pset2/ $ 

I know it can probably be done without the shift function but I wanted to try and use it anyway but that may be my problem. 

 

 

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

 

char shift(char c, char k);

int main(int argc, string argv[])

{
    if (argc !=2)
    {
        printf("%s (key)", argv[0]);
        return 1;
    }
    
    for(int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if (isalpha(argv[1][i]) == 0)
        {
            printf("Usage: %s (key)\n", argv[0]);
            return 1;
        }
    }
        
        string plaintext = get_string("Plaintext: ");
        printf("Ciphertext: ");
        
        int j = 0;
        
    for(int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if (isalpha(argv[1][i]))
        {
            j = j % strlen(argv[1]);
            int ciphertext = shift (plaintext[i], argv[1][j]);
            printf("%c", ciphertext);
            j++;
        }
        
    }
}

char shift(char c, char k)
{
    if isupper(c)
    {
        return c - 'A' + k; 
    }
        
    else if islower(c)
    {
        return c - 'a' + k;
    }
    else
    {
        return c;
    }
}

Please log in or register to answer this question.

Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

195 questions

248 answers

217 comments

4,622 users

7 Online
0 Member And 7 Guest
...