File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <cs50.h>
3
+ #include <string.h>
4
+ #include <ctype.h>
5
+ #include <stdlib.h>
6
+
7
+
8
+ int main (int argc , string argv [])
9
+ {
10
+ //Check key in command line argument
11
+ if (argc < 2 || argc > 2 )
12
+ {
13
+ printf ("Usage: must include a key\n" );
14
+ return 1 ;
15
+ }
16
+
17
+ //Get the key
18
+ int k = atoi (argv [1 ]);
19
+
20
+ //Check the key
21
+ if ( k < 0 )
22
+ {
23
+ printf ("Key must be non-negative\n" );
24
+ return 1 ;
25
+ }
26
+
27
+
28
+ //Declare some variables
29
+ string input = NULL ;
30
+ string uppercaseAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
31
+ string lowercaseAlpha = "abcdefghijklmnopqrstuvwxyz" ;
32
+
33
+ //Ask user for plain text
34
+ do
35
+ {
36
+ input = GetString ();
37
+ }while (input == NULL );
38
+
39
+ //Print out the cipher text
40
+ for (int i = 0 , n = strlen (input ); i < n ; i ++ )
41
+ {
42
+ if (!isalpha (input [i ])) continue ;
43
+
44
+ char ci = input [i ];
45
+
46
+ if (isupper (ci ))
47
+ {
48
+ int offset = ci + k - 'A' ;
49
+ offset = offset % 26 ;
50
+ ci = uppercaseAlpha [offset ];
51
+ input [i ] = ci ;
52
+ }
53
+
54
+ else if (islower (ci ))
55
+ {
56
+ int offset = ci + k - 'a' ;
57
+ offset = offset % 26 ;
58
+ ci = lowercaseAlpha [offset ];
59
+ input [i ] = ci ;
60
+ }
61
+ }
62
+
63
+ printf ("%s\n" , input );
64
+ return 0 ;
65
+ }
You can’t perform that action at this time.
0 commit comments