#include
char *Change(char s[]) {
int i;
for(i = 0; s[i]; ++i) {
if(s[i] >= 'a' && s[i] <= 'z')
s[i] = (26 + (s[i] - 'a' - 3))%26 + 'a';
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] = (26 + (s[i] - 'A' + 3))%26 + 'A';
}
return s;
}
char *Change2(char s[]) {
int i;
for(i = 0; s[i]; ++i) {
if(s[i] >= 'a' && s[i] <= 'z')
s[i] = (26 + (s[i] - 'a' + 3))%26 + 'a';
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] = (26 + (s[i] - 'A' - 3))%26 + 'A';
}
return s;
}
int main() {
char a[] = "dsereaiklfiwieik",b[] = "slASSFGGHHJHKKIUUYUYYHHNJKK";
printf("转换前: %s\n",a);
printf("转换后: %s\n",Change(a));
printf("恢复后: %s\n\n",Change2(a));
printf("转换前: %s\n",b);
printf("转换后: %s\n",Change(b));
printf("恢复后: %s\n",Change2(b));
return 0;
}
还像12那样写,把判断分支改为:
if(a[i]-3>='A' && a[i]-3<='Z')
a[i]-=3;
else if(a[i]+3>='a' && a[i]+3<='z')
a[i]+=3;
即可。