[Fortran] 纯文本查看 复制代码
! 翻译密码。
program char_4
implicit none
integer::i,j,k
character*80::line1,line2
character*1::l1,l2
read *,line1
do k=1,80
l1=line1(k:k)
if ((lge(l1,'A').and.lle(l1,'Z')).or.(lge(l1,'a').and.lle(l1,'z'))) then
i=iachar(l1)
j=i+5
else
if(j>iachar('z').or.(j>iachar('Z').and.j<iachar('Z')+6)) then
j=j-26
l2=achar(j)
line2(k:k)=l2
else
line2(k:k)=l1
end if
end if
end do
print *,line2
end program char_4
调试程序没错,但是例如输入 "HE IS IN BEIJING" 程序没反应,求解~[Fortran] 纯文本查看 复制代码
program char_4
implicit none
integer::j,k
character(len=80)::line1,line2=""
character::t
read(*,"(a80)") line1 !//此处用格式读入,不用表控,否则只读 HE
do k=1,len_trim(line1)
t = line1(k:k)
if ((lge(t,'A').and.lle(t,'Z')).or.(lge(t,'a').and.lle(t,'z'))) then
j = iachar(t) + 5
if( j>iachar('z').or.(j>iachar('Z').and.j<iachar('Z')+6) ) then
j = j - 26
end if
t = achar(j)
end if
line2(k:k) = t
end do
print *,line2
end program char_4