0
//initializing libraries
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

int main(int argc, string argv[])
{
    // Variable declarations
    bool validkey = false;
    string key = "";
    int keylen= 0;
    int input_length = 0;
    string text = "";
    char curr_key ;
  do
    {
        // checking if key is provided 
        if(argc != 2 )
        {   
            //if key is not provided or invalid printing exampl
            printf("Usage: ./caesar k\n");
            return 1;
        }

        else
        {  key = argv[1];

        //checking if key is alphanumeric
            for(int i = 0,z = strlen(argv[1]);i<z;i++)
            {
                while(!isalpha(key[i]))
                {
                     printf("Usage: ./caesar k\n");
            return 1;
                }
            }
            // converting key from string to int using atoi()
           keylen = strlen(key);
            validkey = true;
        }
    } while(!validkey);

    // Read user input
    printf("plaintext: ");
    text = GetString();
    printf("ciphertext: ");
    input_length = strlen(text);
    for(int i = 0,j = 0; i < input_length; i++)
    {  

        if(j>=keylen)
        { j = 0;
            if(isupper(key[j]))
            {
             curr_key = key[j]-65;
            printf("%c",curr_key);
            }
            if(islower(key[j]))
            {
             curr_key = key[j]-97 ;
            printf("%c",curr_key);
            }

        }
        if(j<keylen)
        { 
             if(isupper(key[j]))
            {
             curr_key = key[j]- 65;

            }
            if(islower(key[j]))
            {
             curr_key = key[j]- 97;

            }
            if(isalpha(text[i]))
            {
           j++;
            }
        }
        // primary logic
        if(isalpha(text[i]))
        {
            // logic lowercase
            if(islower(text[i]))
            {
                printf("%c", ((((text[i] - 97)+curr_key)% 26)+97));
            }
            // logic for uppercase
            else
            {
                printf("%c", ((((text[i] - 65)+curr_key)% 26)+65));
            }
        }
        // other alphanumeric characters except aplhabet
        else
        {   
            printf("%c", text[i]);
        }
    }

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

enter image description here

1

It looks like you left a debugging printf here printf("%c",curr_key);. The "thing" that is printing is a Unicode 1. And it's only printing when the key "resets" to 0. Since all the tests use b/B as the first character, the value of curr_key is always 1.

The reason you do not see the extra characters when you run the program is because they are "non-printing" characters. If you want to "see" what check50 "sees" a command like this will show:

echo "world, say hello!" | ./vigenere baz | cat -v

The echo sends the literal to ./vigenere as input, then the output is displayed with cat -v. The -v switch of cat (my personal favorite) says "show non-printing".

You must log in to answer this question.

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