0

im stuck now, I cant see it. Its all working and ciphers correctly, but whenever the first index of plaintext is a capital, it seems to print it twice?

I have gone through it line by line for ages and I really cant see it.

Can you please point me in the right direction?

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

int key;

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        key = atoi(argv[1]); //atoi stores string as int
        printf("plaintext:  ");
        string plain = get_string(); //get plaintext
        printf("ciphertext: ");

            for (int i = 0; i < strlen(plain); i++) //for each char in plaintext
            {
                if (isupper(plain[i]))
                {
                    plain[i] = (((plain[i] - 65) + key) % 26) + 65;
                    printf("%c", plain[i]);
                }

                if (islower(plain[i]))
                {
                    plain[i] = (((plain[i] - 97) + key) % 26) + 97;
                    printf("%c", plain[i]);
                }
                else
                {
                    printf("%c", plain[i]);
                }
            }
            printf("\n");
            return 0;
    }
    else
    printf("Usage: ./caesar k\n");
    return 1;
}
1
  • Been playing around a bit, I swapped islower and isupper around and now having the problem where lowercase is getting double printed...So its obviously something to do with my looping or the 'order' my code runs, its the same code almost but with 2 different outputs..Its only happening on the first element of the array – Jamie Evans Sep 29 '17 at 13:42
0

Answered my own question, I dont know why I decided to print a char after every if statement, decided to print the char at the end. it works, dont know why it didnt work before, I just know now it works, and clearly a better way of doing it

You must log in to answer this question.

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