จงเขียนโปรแกรมเพื่อหา Acronym (ตัวย่อ)
ประโยคที่มีหนึ่งคำหรือมากกว่าหนึ่งคำแบ่งคำด้วยอักขระช่องว่าง (space)
ตัวย่อของประโยคถูกสร้างขึ้นโดยใช้ตัวอักษรตัวแรกของแต่ละคำจากประโยค ยกเว้นคำพิเศษต่อไปนี้จะถูกละเว้น:
'i', 'of', 'the', 'on', 'at', 'for', 'with', 'a', 'an' and 'in'.
อย่างไรก็ตาม ถ้าคำพิเศษคือคำคำแรกในประโยค ให้เราพิจารณาว่าเป็นคำที่สำคัญมากและรวมไว้ในตัวย่อด้วย
Task
เขียนโปรแกรมเพื่อสร้างตัวย่อจากประโยคที่ป้อนเข้าไป ตัวย่อต้องเป็นตัวใหญ่เสมอ
Input
ประโยคที่ประกอบด้วยตัวอักษร ('a'.....'z') และ space
Output
Example 1
Input
|
Output
|
micro soft
|
MS
|
Example 2
Input
|
Output
|
that is what friend are for
|
TIWFA
|
Example 3
Input
|
Output
|
on the way of computer engineering
|
OWCE
|
Credit : แบบฝึกหัดการเขียนโปรแกรมนี้ เป็นแบบฝึกหัดที่ได้มาจากหนังสือ และข้อสอบของมหาวิทยาลัยต่างๆ ขอขอบคุณไว้นะที่นี้ด้วยครับ
Source Code
package com.olanlab.share.exam;
import java.util.Scanner;
public class Acronym {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String sentence;
String offwords[] = {"i", "of", "the", "on", "at", "for", "with", "a", "an", "in"};
sentence = scan.nextLine();
String words[] = sentence.split(" ");
for (String word : words) {
boolean skip = false;
for (String offword : offwords) {
if (word.equals(offword)) {
skip = true;
}
}
if (skip) {
continue;
} else {
System.out.print(word.toUpperCase().charAt(0));
}
}
}
}
แบบฝึกหัดระดับ 1 ดาวอื่นๆ
ตัวย่อ
แก้ว 3 ใบ
ดาว