1. Vigenere Cipher

Hey I'm taking the Hardvard CS50 course through ItunesU and I'm working on writing a code that will encrypt a message using the vigenere cipher which uses the
equation... Ci=(Pi+Kj)%26 where P is plaintext and K is the word to encrypt by.
I almost have it correct but I can't figure out how to make the ASCII letters wrap around from Z back to A and not go into other ASCII symbols. I've gotten the plain text to wrap around but not the key. Heres the code:

Code:
```#include <stdio.h>
#include <string.h>

int main(void)
{
printf("What message would you like to encrypt?: \n");
char p[50];
fgets(p, 50, stdin);
printf("What would you like your keyword to be?(do not use caps): \n");
char key[15];
fgets(key, 15, stdin);
int i, n, j;
for(i = 0, j = 0, n = strlen(p); i < n; i++, j++)
{
if(p[i] <= 'A' && p[i] >= 'Z')
printf("%c", (((p[i] - 'A') + (key[j] - 'A' ) %26) + 'A'));
if(p[i] <= 'a' && p[i] >= 'z')
printf("%c", (((p[i] - 'a') + (key[j] - 'a' ) %26) + 'a'));
else if(p[i] >= 'A' && p[i] <= 'Z')
printf("%c", (((p[i] - 'A') + (key[j] - 'A') %26) + 'A'));
else if(p[i] >= 'a' && p[i] <= 'z')
printf("%c", (((p[i] - 'a') + (key[j] - 'a') %26) + 'a'));
else
printf("%c", p[i]);
}
printf("\n");
return 0;
}```

2. > if(p[i] <= 'A' && p[i] >= 'Z')
Do you know this can never be true?

> printf("What would you like your keyword to be?(do not use caps): \n");
...
> printf("%c", (((p[i] - 'A') + (key[j] - 'A') %26) + 'A'));
This is wrong, if your key is always lower case.

3. You're going to drive yourself crazy with all the different cases between the plain text beting upper or lower case (or a symbol), and the key being upper or lower case separately. Why not write a function like "convert_char_to_index" that takes a char and returns 0 for a (or A), 1 for b (or B), etc. and use that to reduce the complexity of your conversion code?

4. Ahhhh OK I will try that. Thanks for the help.

5. I'm really new to this. How would I do that tabstop?

6. I usually use a keyboard.

You need to write a function that takes a char and returns an int. You'll need to decide what you want to happen for things like ' ' and '.' and other punctuation marks. For the letters you'll need to return 0 through 25.

7. Originally Posted by wexvenemium
I'm really new to this. How would I do that tabstop?
Well you could always do like he suggested:
Originally Posted by tabstop
write a function like "convert_char_to_index"
Is that the confusing part?
Originally Posted by tabstop
that takes a char
Or that?
Originally Posted by tabstop
and returns 0 for a (or A), 1 for b (or B), etc.
Or maybe that? I don't see you actually trying anything, so all I can do here is guess at why you are so bewildered.

Quzah.

8. Don't be a dick.

9. I think a link to a function pointer tutorial would suffice here guys.
"what have you tried?" is the way to not come across as being the opposite of helpful.

Sometimes you catch people on a bad day.

10. Thanks iMalc. That tutorial really helped.

11. I've also been trying to use atoi to convert the chars to ints, but I can't figure out how that would work for an array such as int[25] and define 'a' - 'z' and 'A' - 'Z' to 0-25 of the array. So I have a feeling that writing a separate function that does more than convert one char to int at a time would be the only way. Tell me if I'm wrong....

Here's my updated code... I took the atoi out cuz I kept getting bus errors because I think I was trying to assign an array to an int or some ........ like that...
The code is encrypting successfully with the exception that 'z' or 'Z' will not wrap around to 'a' or 'A'. I finally got it to stop giving me other various ascii symbols though.

Code:
```#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
{
if(argc != 1)
{
printf("........ You!\n");
return 1;
}
}
char p[50];
if(argc	= 1)
printf("What message would you like to encrypt?\n");
fgets(p, 50, stdin);
char key[15];
printf("What would you like your keyword to be?\n");
fgets(key, 15, stdin);
int i, j, n;
for(i = 0, j = 0, n = strlen(p); i < n; i++, j++)
{
char a = (((p[i] - 'A') + (key[j] - 'a') % 26) + 'A');
char b = (((p[i] - 'a') + (key[j] - 'a') % 26) + 'a');
if(a >= 'A' && a <= 'Z')
printf("%c", a);
if(b >= 'a' && b <= 'z')
printf("%c", b);
else
printf("%c", p[i]);
}
printf("\n");
return 0;

}```

12. atoi takes a textual representation of a number, as in "145" as a string, and returns the value 145. It is not going to help you even a little bit for this problem.

(Also the code you have wraps around correctly, so you should give some example of what you are concerned about.)

13. Originally Posted by wexvenemium
Don't be a dick.
Originally Posted by wexvenemium
Thanks iMalc. That tutorial really helped.
Wait a minute. I specifically ask you what part of making a function you don't understand, and you get your panties in a twist, but then he does the same thing and you thank him? What a dumbass.

Quzah.

14. iMalc gave me some help and he wasn't totally condescending, like you were. I was just looking for a little help; I wasn't really looking to be scoffed at by ..........s like you. I don't know why you need to make people feel inferior over internet forums. Did your father rape you? You know there are people that can help you get closure on your traumatic past. Anyway, your highness, why don't you take your supreme knowledge and awe inspiring intellect and just .......... off.

15. OK - enough.