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

int main(int argc, string argv[])
{
    if(argc != 2) // RETURNS TO 1 if not 2 strings
    {
        printf("Usage: ./caesar k\n");
        return 1;
    }  
    // calculate length of string and make sure it is alpha
    string keyphrase = argv[1];
    // Get string length and calculate characters and loop string
    for (int j = 0, l = strlen(keyphrase); j < l; j++)
    {
        if(!isalpha(keyphrase[j]))
        {
            printf("Please ensure all characters are alphabetical.\n");
            return 1;
        }
        else if(isalpha(keyphrase[j]))
        {
            printf("plaintext: ");
            string s = get_string();
            printf("ciphertext: ");

            // CUT OFF HERE JUST IN CASE and also cipher plain text from here on out
            for(int i = 0, n = strlen(s); i < n; i++)
            {
                if(isalpha(s[i]) && isupper(s[i])) 
                {
                    // convert asci to alphbetical for upper case

                    int cipher = s[i]-65;
                    int c = (cipher + keyphrase[j % l]- 65) % 26;
                    printf("%c", c+65);
                }
                //convert for lower case
                else if(islower (s[i]) && isalpha(s[i]))
                {
                    int cipher2 = s[i]-97;
                    int d = (cipher2 + keyphrase[j % l] - 97) % 26;
                    printf("%c", d+97);
                }
                //PRESERVE DIGITS SPACES AND PUNCTS NEW HEERE
                else if(isdigit (s[i]))
                {
                    printf("%c", s[i]);
                }
                else if(ispunct (s[i]))
                {
                    printf("%c", s[i]);
                }
                else if(isspace (s[i]))
                {
                    printf("%c", s[i]);
                }
                // PASTED CODE ENDS HERE JUST IN CASE

            }
            //print space after i for loop
            printf("\n");
            return 0;
        }
    }
}

THis is how far I got so far in vigenere and I am stuck I really dont know how to fix my keyword and I just dont understand :(

1 Answer 1

1

Whole plaintext/ciphertext should not be in the else path, but after the loop. You want to do that only after ensuring that the whole key is alphabetic.

You need to increment the index for the key string only after encrypting a character.

Also, don't use isdigit and similar, use either the else path of previous isalpha, or if with !isalpha, catches them all.

3
  • ah okay ill try this out
    – Roundabout
    Commented Jan 30, 2017 at 16:23
  • So should i just erase my (isalpha(keyphrase[j])) in my else if function?
    – Roundabout
    Commented Jan 31, 2017 at 3:25
  • Inside the first for loop, there should be no else. Nothing to do on alphabetic character at that place. Maybe you could lower- or upper-case current character in the else (optional) , but don't ask for plaintext in the first loop.
    – Blauelf
    Commented Jan 31, 2017 at 8:57

You must log in to answer this question.

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