シーザー暗号
Posted: 2009年1月23日(金) 12:34
大学の課題で今シーザー暗号を作っています。
一様プログラムはできたのですがprintf("3と入力してください: ");で3と入力せずに3こづらしたいのですがどのようにこのプログラムを改良すればよいですか?
#include <stdio.h>
#include <string.h>
char *shift_char(char x[/url], int y){
static const char table[/url] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
static const int table_len = (sizeof(table)-1) / 2;
char *p;
y %= table_len;
for(p=x;*p;p++)
*p = *(strchr(table, *p) + y);
return x;
}
int main(void){
char input[301];
int count;
printf("アルファベットを入力してください: ");
scanf("%s", input);
printf("3と入力してください: ");
scanf("%d", &count);
printf("%s", shift_char(input, count));
return 0;
}
一様プログラムはできたのですがprintf("3と入力してください: ");で3と入力せずに3こづらしたいのですがどのようにこのプログラムを改良すればよいですか?
#include <stdio.h>
#include <string.h>
char *shift_char(char x[/url], int y){
static const char table[/url] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
static const int table_len = (sizeof(table)-1) / 2;
char *p;
y %= table_len;
for(p=x;*p;p++)
*p = *(strchr(table, *p) + y);
return x;
}
int main(void){
char input[301];
int count;
printf("アルファベットを入力してください: ");
scanf("%s", input);
printf("3と入力してください: ");
scanf("%d", &count);
printf("%s", shift_char(input, count));
return 0;
}