Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, string argv[])
- {
- //verify that have the correct number of arguments
- if (argc!=2){
- printf("Please dont forget to add ONE alphabetical argument\n");
- return 1;
- }
- string plaintext;
- string key = argv[1];
- //verify that the key is alphabetical
- if (isalpha(key)) {
- plaintext = GetString();
- }
- else {
- printf("Please use am alphabetical key argument\n");
- return 1;
- }
- //encipher here
- int nonalpha = 0;
- int keylength = strlen(key);
- int keynum;
- for (int i = 0, n=strlen(plaintext); i < n; i++)
- {
- int keyposition = (i - nonalpha) % keylength;
- if (islower(key[keyposition])){
- keynum = (int)(key[keyposition] -97);
- }
- else {
- keynum = (int)(key[keyposition] -65);
- }
- if (isalpha (plaintext[i])){
- if (islower(plaintext[i])){
- int c = (((plaintext[i] - 97 + keynum) %26) +97);
- printf("%c", c);
- }
- else {
- int c = (((plaintext[i] - 65 + keynum) %26) +65);
- printf("%c", c);
- }
- }
- else {
- printf("%c", plaintext[i]);
- nonalpha = nonalpha + 1 ;
- }
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement