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

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        int n = atoi(argv[1]);

        printf("plaintext: ");
        string s = get_string();
        printf("ciphertext: ");

        for (int i = 0, j = strlen(s); i < j ; i++)
        {
            if (isalpha(s[i]))
            {
                if (isupper(s[i]))
                {
                    printf("%c", ((('s[i]' - 65) + n) % 26) + 65);
                }
                else
                {
                    printf("%c", ((('s[i]' - 97) + n) % 26) + 97);
                }
            }
        }
    }
    else
    {
        printf("Error!\n");
        return 1;
    }
    printf("\n");
    return 0;
}

I am having difficulty with the part in the caesar cipher part where I have to convert the ascii index to alphabetical index. I spent a long time trying to figure out why the formula won't work. The code compiles and runs fine. But, if my key is 5, and I were to input "hello", then all I would get are five lots of v's ("vvvvv"). If I input "HELLO", I would get give lots of B's ("BBBBB").

It is most likely wrong, but here's is my understanding of it: taking "hello" for example, h would have ascii index of 104. Firstly, since i = 0, we deal with "h", since it is a lower case, the following would run:

printf("%c", ((('s[i]' - 97) + n) % 26) + 97);

so, the int of s[i] would be 104, right? ((104-97)+5) % 26 = 12. Add back on the 97, I would get 109. printf the char of that should get me a lowercase m?

Could someone please correct me on my way of understanding this, and maybe provide a workaround for this. Thank you.

4

Your formula is correct as well as the understanding of the formula, only that you are mixing two things in a wrong way, look carefully at the following expression:

((('s[i]' - 97) + n) % 26) + 97)

What is s[i], and what is 's[i]'? I have to say that it is the first time that I encounter this situation, probably 's[i]' is interpreted by the compiler as a string, instead of a character, since it does not appear as an error, we can do for example 'A', in the program that will be interpreted as a character, and internally it will be treated as an integer, but we can not do 's[i]' and expect it to be a character Your program will work perfectly if you do:

(((s[i] - 97) + n) % 26) + 97)
5
  • So having the ' ' around it treats it as a string? Whereas, C would have automatically treated the s[i] as an int because it's smart enough to know I want to minus 97 from it? – Knovolt Jun 10 '17 at 19:48
  • Is the first time I see the expression 's [i]', referring to an element of an array, then it is a supposition that is considered as a string, as for the second question all elements of a program in C, at end are treated as numbers, also a character, so we can do things like "add" characters as we do in caesar, what matters is that when using printf we use support %c, the function does the work of printing the associated character With the corresponding ASCII code – MARS Jun 10 '17 at 20:10
  • If I try to print 'A' + 2 I would get 67. But, why is it I don't need the ' ' around the s[i]? If my string were to be "And" and i = 0, would it not be the same as saying 'A' by doing 's[i]'? – Knovolt Jun 10 '17 at 23:17
  • S is a string then its elements are already characters, quotes are not necessary – MARS Jun 11 '17 at 5:09
  • Ah, that's right. Thanks for the explanations! – Knovolt Jun 11 '17 at 17:00
0

Preferebly, you should go for explicit casting which casts a one data type to another. That is you can (int) function to convert from the string charachter s[i] to its ascii value . After this you can apply the rest of the formula in same way as above .Happy coding!

You must log in to answer this question.

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