返回列表 發帖

409 字串與檔案處理 (字串加密)

1. 題目說明:
請依下列題意進行作答,使輸出值符合題意要求。

2. 設計說明:
請撰寫一程式,讀取read.txt檔案內容,將檔案中的小寫英文字母加密,加密方法為所有小寫字母向後偏移2個字母,並將結果寫入至write.txt檔案。

提示:若使用 Java 語言答題,請以「JP」開頭命名包含 main 靜態方法的 class,評測系統才能正確評分。

3. 輸入輸出:
輸入說明
讀取read.txt檔案內容

輸出說明
寫入至write.txt檔案

範例輸入



範例輸出



附件:read.txt

本帖隱藏的內容需要回復才可以瀏覽

python
  1. f = open('read.txt','r')
  2. line = f.read()
  3. f.close()

  4. wf = open('write.txt','w')
  5. for i in line:
  6.     if i == 'y':
  7.         wf.write('a')
  8.     elif i == 'z':
  9.         wf.write('b')
  10.     else:
  11.         wf.write(chr(ord(i)+2))


  12. wf.close()
複製代碼

  1. with open("read.txt",'r') as r:
  2.     out=""
  3.     s=r.readline()
  4.     for i in range(len(s)):
  5.         out+=chr(ord(s[i])+2)
  6. with open("write.txt",'w') as w:
  7.     w.write(out)
複製代碼

TOP

  1. out=""
  2. with open('read.txt','r')as a:
  3.     n=a.read()
  4.     for i in range(len(n)):
  5.         out+=chr(ord(n[i])+2)
  6. with open('write.txt','w')as b:
  7.     b.write(out)
複製代碼
回復 1# 鄭繼威

TOP

out=""
with open("read (4).txt","r") as r:
    s=r.readline()
    for i in range(len(s)):
        out+=chr(ord(s[i])+2)
        
with open("write.txt", "w") as w:
    w.write(out)

TOP

  1. out=""
  2. with open(r'read (2).txt','r') as r:
  3.     str1=r.readline()
  4.     for i in range(len(str1)):
  5.         str2=ord(str1[i])+2
  6.         str3=chr(str2)
  7.         out+=str3
  8. with open(r'write.txt','w') as w:
  9.     w.write(out)
複製代碼

TOP

  1. out=""
  2. with open("read.txt",'r') as r:
  3.     s=r.readline()
  4.     for i in range(len(s)):
  5.         out+=chr(ord(s[i])+2)
  6. with open("write.txt",'w') as w:
  7.     w.write(out)
複製代碼

TOP

  1. out=""

  2. with open("read.txt",'r') as r:   
  3.     s=r.readline()
  4.     for i in range(len(s)):
  5.         out+=chr(ord(s[i])+2)

  6. with open("write.txt",'w') as w:
  7.     w.write(out)
複製代碼

TOP

返回列表