返回列表 發帖

410 字串與檔案處理 (字首轉大寫)

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

2. 設計說明:
請撰寫一程式,讓使用者輸入一個正整數,並讀取read.txt檔案內容,read.txt檔案中包含多列英文句子。若輸入值為n,則讀取n列read.txt檔案內容,將n列中的每個英文單字字首轉為大寫再輸出,並寫入至write.txt檔案。

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

3. 輸入輸出:
輸入說明
一個正整數n,並讀取read.txt檔案內容

輸出說明
將n列中的每個英文單字字首轉為大寫再輸出,並寫入至write.txt檔案

範例輸入
5



範例輸出
Hello World!
What Is The Weather Like Out There?
Are You Ok?
What's Today's Date?
Excuse Me!




附件:read.txt

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

Python
  1. f = open('read.txt','r')
  2. line = f.readlines()
  3. f.close()
  4. wf = open('write.txt','w')

  5. n = int(input())
  6. for i in line[:n]:
  7.     i = i.strip()
  8.     i = i.split(' ')
  9.     for idx, word in enumerate(i):
  10.         word = word.capitalize()
  11.         if idx == len(i) - 1:
  12.             print(word)
  13.             wf.write(word + '\n')
  14.         else:
  15.             print(word, end = ' ')
  16.             wf.write(word + ' ')
  17.     # print(i)
  18.         # wf.write(word+'\n')
  19. wf.clo
複製代碼

返回列表