Cin-browser interpreter · runs locally
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h> #include <string.h> char shift(char c, int n) { if (c >= 'a' && c <= 'z') return 'a' + (c - 'a' + n) % 26; if (c >= 'A' && c <= 'Z') return 'A' + (c - 'A' + n) % 26; return c; } int main() { string key, msg; printf("Enter keyword: "); scanf("%s", &key); printf("Enter message: "); scanf("%s", &msg); int klen = strlen(key), ki = 0; for (int i = 0; msg[i] != 0; i++) { char k = key[ki % klen]; int n = (k >= 'a' && k <= 'z') ? k - 'a' : k - 'A'; if ((msg[i] >= 'a' && msg[i] <= 'z') || (msg[i] >= 'A' && msg[i] <= 'Z')) { printf("%c", shift(msg[i], n)); ki++; } else { printf("%c", msg[i]); } } printf("\n"); return 0; }
30 lines
Output
Write some code, then press Run. Stdout and errors appear here.