0

Below is my vigenere code for enciphering text, but the enciphering key does not wraparound.

/* a program, vigenere, that encrypts messages using Vigenere’s cipher*/

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

int main(int argc, string argv[])
{
    //Checks for the required argc
    if(argc != 2)
    {
        printf("Command line argument error, please insert key!\n");
        return 1;
    }

    for(int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if(isalpha(argv[1][i]) == 0)
        {
            printf("key argv[1] has to be alphabetic\n");
            return 1;
        }
    }
    //Prompts user for input of plaintext
    char* Plain_text = get_string();
    for(int i = 0, m = strlen(Plain_text); i < m; i++)
    {
        //Checks if plaintext is alphabetic
                if(isalpha(Plain_text[i]))
                {
                    if(isupper(Plain_text[i]))
                    {
                            argv[1][i] = ((argv[1][i] - 'A'));

                            //Alphabetic wraparound for upper case letters
                            Plain_text[i] = ((Plain_text[i] - 'A') + argv[1][i]) % 26;
                            printf("%c", Plain_text[i] + 'A');
                    }
                    else
                    {
                        argv[1][i] = ((argv[1][i] - 'a'));
                        //Alphabetic wraparound for lower case letters
                        Plain_text[i] = ((Plain_text[i] - 'a') + argv[1][i]) % 26;
                        printf("%c", Plain_text[i] + 'a');
                    }
                }
                else
                printf("%c", Plain_text[i]);
    }
    return 0;
}

can anyone help me point out what i did wrong?.Thanks

2
  • An easy way to properly format your code: On initial post, after you paste the code, select it, then click the {} icon in the format bar. Or you can edit the post and do the same (select code, click {} icon in the format bar). Commented Feb 1, 2017 at 12:43
  • @Jerey I'm confused - why do you do argv[1][i] = ((argv[1][i] - 'A'));? Commented Feb 1, 2017 at 14:13

0

You must log in to answer this question.

Browse other questions tagged .