0

below is my vigenere code, it sorts of prints just the first character of the string input and it gets stuck after that for some reason I can seem to figure out.

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

int main(int argc, string argv[])
{
    if(argc != 2)
    {
        printf("Usage: ./vigenere k\n");
        return 1;
    }
    for(int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if(!isalpha(argv[1][i]))
        {
            printf("string inputted is not alphabetic\n");
            return 1;
        }
    }

        printf("plaintext: ");
        string plain_text = get_string();

        printf("ciphertext: ");
        for(int i = 0, n = strlen(plain_text); i < n; i++)
        {
            int m = strlen(argv[1]);
            int j = 0;
            while( i < n)
            {
                if( j == m)
                {
                    j = 0;
                }
                if(isupper(plain_text[i]))
                {
                    plain_text[i] = (((plain_text[i] - 'A') + ((int) argv[1][j] - 'A')) % 26);
                    printf("%c", plain_text[i] + 'A');
                }
                else if(islower(plain_text[i]))
                {
                    plain_text[i] = (((plain_text[i] - 'a') + ((int) argv[1][j] - 'a')) % 26);
                    printf("%c", plain_text[i] + 'a');
                }
                else
                {
                    printf("%c", plain_text[i]);
                }
                j++;
            }
            while(i < n);
        }
}

any insight as to why its not fully implementing would be greatly appreciated.

0
0

while(i < n) <- you seem to have a while loop definition issue (you have it repeating twice, I think you mean to use

`do{
 %stuff
 }(while);`

, and the i used in the loop does not appear to be incremented in the loop itself.

Perhaps you can start there.

You must log in to answer this question.

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