Skip to Content
CoursesCSC122โปรแกรมเข้ารหัสข้อความ

โปรแกรมเข้ารหัสข้อความ

โจทย์

You have a position in communication squad. Your task is to encrypt a communication message by reverse every word in the message and make every word title case (use upper case for the first letter). The first line of input is number of words in a message. The second line is a message.

InputOutput
5
Good Luck to Your Exam
Doog Kcul Ot Ruoy Maxe

โค้ด

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int size = scanner.nextInt(); String[] inputStrings = new String[size]; for (int i = 0; i < size; i++) { inputStrings[i] = scanner.next(); } String[] processedStrings = new String[size]; for (int i = 0; i < size; i++) { processedStrings[i] = reverseAndTransform(inputStrings[i]); } for (String str : processedStrings) { System.out.print(str + " "); } scanner.close(); } private static String reverseAndTransform(String str) { String transformed = ""; String lowerCaseStr = str.toLowerCase(); for (int i = str.length() - 1; i >= 0; i--) { char currentChar = (i == str.length() - 1) ? Character.toUpperCase(lowerCaseStr.charAt(i)) : lowerCaseStr.charAt(i); transformed += currentChar; } return transformed; } }

คำอธิบาย

ปรับปรุงล่าสุด