0

One of the lines of my code is this -

 if (isupper(plaintext[j]))
    {
         int cap_end = (plaintext[j] - (int) 'A' + atoi(argv[1]))%26 + (int) 'A';
         printf("%c", (char) cap_end);
    }

This lines compines however here I am trying to do something which Brian mentioned in the Walkthrough and that is to convert ASCII Index to Alphabet Index. However plaintext[j] is a string and I am subtracting a int from it shouldn't it not work? As the string a array of chars so shouldn't I need to convert it into a int first to use or does C do it for me and if does then why do I need to write A like the way I have done? I tried simply subtracting A or 'A' and it didn't work. Also (int) A didn't work only (int) 'A' worked is there a reason behind the ' ' used around A?

1 Answer 1

1

The single quotes around something say that "this is a literal character". Without the single quotes, or double quotes (indicating a string), it's a variable name.

One thing you need to learn is this. A char is also treated by C as a single byte signed integer. It is not necessary to cast a char as an int. It will be treated as an int automatically, if appropriate (i.e., is in a mathematical equation.)

My next comment is this. Why wouldn't that line of code compile? There's nothing technically wrong with it. Whether it does what you want it to do is a totally different story. (I honestly haven't looked into that this time. That's part of your learning experience.)

Also, why type cap_end as an int and not a char?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • thanks again man :) it's been a real help Commented Oct 8, 2020 at 7:54

You must log in to answer this question.

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