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

int main(int argc, string argv[])
{   
    if(argc != 2)
    {
    printf("Try again. Your key was incorrect.\n");
    return 1;
    }
    int k = atoi(argv[1]);
    if(k <= 0)
    {
    printf("Try again. Your key was not in the accepted range of 0 to 25, or was a string.\n");
    return 1;
    }
    if(k >= 26)
    {
    printf("Try again. Your key was not in the accepted range of 0 to 25, or was a string.\n");
    return 1;
    }
    string t = GetString();
    int i = 0;
    for (int n = strlen(t); i < n; i++)
    {
    if(isalpha(t[i]))
    {
        if(isupper(t[i]))
        {
            int l = t[i];
            l = l - 65;
            l = (t[i] + k)%26;
            l = l + 65;
            char c = l;
            printf("%c", c);
            }
        if(islower(t[i]))
            {
            int v = t[i];
            v = v - 97;
            v = (t[i] + k)%26;
            v = v + 97;
            char z = v;
            printf("%c", z);
            }

        }
        printf("%c", t[i]);
    }
    printf("\n");
}

The output of this code is UCfhceceqsce with a key of 5 and a input of Cheese. The desired output is Hmjjxj. This is for caesar.c. Thanks in advance.

1 Answer 1

1

printf("%c", t[i]); This line of code prints out each char again, even though you already printed out the alphabetical ones at the end of the isupper and islower if conditions. It explains why you get 12 letters out when you only input 6 letters.

Good luck!

3
  • Thanks! Any idea about the other letters being off?
    – mmoore
    Commented Jul 23, 2015 at 17:11
  • Nvm, fixed it! Thanks again!
    – mmoore
    Commented Jul 23, 2015 at 18:23
  • If my answer helped you, could you please accept it?
    – i_am_david
    Commented Jul 23, 2015 at 18:29

You must log in to answer this question.

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