求助,for循环里出了问题,但我感觉没问题

P1914 小书童——凯撒密码

nico2002 @ 2024-03-13 11:47:26

#include<stdio.h>
#include<string.h>

int main(){
    int n;
    scanf("%d",&n);
    getchar();
    char s[105];
    gets(s);
    int len=strlen(s);
    for(int i=0;i<len;i++){
        s[i]=s[i]+n;
        if(s[i]>'z')
            s[i]=s[i]-26;
    }
    printf("%s\n",s);
    return 0;
}   

by quxiangyu @ 2024-03-13 12:16:50

@nico2002 求关注இ௰இ

#include<bits/stdc++.h>
using namespace std;
char a[10005];
int b[10005];
int n,i,la;
int main() {
    cin >> n >> a;
    la = strlen(a);
    for(i = 0 ; i < la ; ++i)
        b[i] = a[i];
    for(i = 0 ; i < la ; ++i)
        b[i]  = b[i] + n;
    for(i = 0 ; i < la ; ++i) {
        if(b[i] > 122)
            b[i] = b[i] % 122 + 96;
    }
    for(i = 0 ; i < la ; ++i) {
        a[i] = b[i];
        cout << a[i];
    }
    return 0;
}

|