वॉवेल लेटर से शुरू होने वाले शब्द को खोजने के लिए:
String क्लास की split() विधि दिए गए स्ट्रिंग को String क्लास की split() विधि का उपयोग करके स्ट्रिंग्स के ऐरे में विभाजित करती है।
फॉर लूप में प्राप्त ऐरे के प्रत्येक शब्द के माध्यम से ट्रैवर्स करें।
प्राप्त ऐरे में प्रत्येक शब्द के पहले कैरेक्टर को charAt() विधि का उपयोग करके प्राप्त करें।
if लूप का उपयोग करके वेरिफाई करें कि कैरेक्टर किसी वॉवेल के बराबर है या नहीं, यदि हां तो शब्द प्रिंट करें।
उदाहरण
मान लें हमारे पास निम्नलिखित कंटेंट वाली एक टेक्स्ट फ़ाइल है:
Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
निम्नलिखित Java प्रोग्राम इस फ़ाइल के सभी शब्दों को प्रिंट करता है जो वॉवेल लेटर से शुरू होते हैं।
import java.io.File;
import java.util.Scanner;
public class WordsStartWithVowel {
public static String fileToString(String filePath) throws Exception {
Scanner sc = new Scanner(new File(filePath));
StringBuffer sb = new StringBuffer();
String input = new String();
while (sc.hasNextLine()) {
input = sc.nextLine();
sb.append(input);
}
return sb.toString();
}
public static void main(String args[]) throws Exception {
String str = fileToString("D:\\sample.txt");
String words[] = str.split(" ");
for (int i = 0; i < words.length; i++) {
char ch = words[i].charAt(0);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == ' ') {
System.out.println(words[i]);
}
}
}
}
आउटपुट
originated idea exists a of on-line and at own of