Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- int main (int argc, string argv[]){
- // testing if user has put correct number of arguments in command-line
- if (argc != 2){
- return 1;
- }
- // transforming the string containing the key into an actual integer
- int k = atoi(argv[1]);
- //getting the plain text from the user
- string plaintext = GetString();
- //declaring the string that will contain the cypher text
- string cipher[strlen(plaintext)];
- int letter;
- for (int i=0, j= strlen(plaintext); i<j; i++)
- {
- // getting letter to be changed
- letter = (int) plaintext[i];
- // checking if character is actually alphabetic
- if (isalpha(plaintext[i])){
- letter = (letter) + (k% 26);
- // checking if wrap round from Z to A is needed
- if (isupper(plaintext[i])){
- if (letter>90){
- letter= 65+ letter-91;
- }
- }
- if (islower(plaintext[i])){
- // checking if wrap round from z to a is needed
- if (letter>122){
- letter= 97+ letter-123;
- }
- }
- cipher[i]= (string) letter;
- printf("%c", (char) cipher[i]);
- }
- else{
- printf("%c", (plaintext[i]));
- }
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement