- 316 名前:デフォルトの名無しさん mailto:sage [2009/10/25(日) 10:52:13 ]
- >307
hashとかmapが使えるとすげー楽なのだけれど、Cには無いので代わりの方法をやってみた。 要はD→A、C→Z・・・の写像を定義してあげればよい。mapだとそのままこの写像が定義できる。 Cではできないので、インデックスに変換してあげている感じ。 #include <stdio.h> #include <string.h> #define ELENUM(array) (sizeof(array) / sizeof(array[0])) /* A, B, C...,Zを0, 1, 2, .. 25に変換。 変換できない場合-1 */ int alphIndexing(char ch) { char alph[] = "ABCDEFCHIJKLMNOPQRSTUVWXYZ"; int idx; for (idx = 0; idx < ELENUM(alph); idx++) { if (alph[idx] == ch) { return idx; } } return -1; } int main(int argc, char **argv) { const char aCaesarTbl[] = "*XYZABCDEFCHIJKLMNOPQRSTUVW"; const char *pConvert = (aCaesarTbl + 1); int idx; printf("input : %s\n", argv[1]); printf("encoded: "); for (idx = 0; idx < strlen(argv[1]); idx++) { putchar(pConvert[alphIndexing(argv[1][idx])]); } putchar('\n'); return 0; }
|

|