Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- /*
- bool all_alpha(const char *str){
- char c;
- while ((c = *str++) != '\0')
- if (!isalpha(c))
- return false;
- return true;
- }*/
- int main(int argc, string argv[]){
- string key;
- int textLength;
- string text;
- int shift;
- char symb;
- //if user did not provide an argument
- if (argc == 1 || argc > 2){
- return 1;
- //if argv[1] provided - get a key
- } else {
- key = (argv[1]);
- }
- //get text for encoding
- text = get_string("Enter text: ");
- textLength = strlen(text);
- printf("ciphertext: ");
- //filtering loop
- int j = 0;
- for (int i = 0; i < textLength; i++){
- if (islower[key[j]])
- shift = key[j] - 'a'
- else
- shift = key[j] - 'A'
- if (isaplha(text[i])) {
- if (islower(text[i]))
- symb = ((((text[i] - 'a') + shift) % 26) + 'a');
- else
- symb = ((((text[i] - 'A') + shift) % 26) + 'A');
- printf("%c", symb);
- j++;
- } else
- printf("%c", text[i]);
- if (j == (strlen(key)))
- j=0;
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement