এই নিবন্ধে আমরা বুঝবো কিভাবে জাভাতে স্বরবর্ণ এবং ব্যঞ্জনবর্ণ গণনা করতে হয়। 'a', 'e', 'i', 'o', 'u' এই অক্ষরগুলোকে স্বরবর্ণ (Vowels) বলা হয় এবং বাকি সব অক্ষরগুলোকে ব্যঞ্জনবর্ণ (Consonants) বলা হয়।
নিচে এর একটি প্রদর্শন দেওয়া হলো:
ইনপুট
মনে করুন আমাদের ইনপুট হলো:
Hello, my name is Charlie
আউটপুট
প্রত্যাশিত আউটপুট হবে:
The number of vowels in the statement is: 8 The number of consonants in the statement is: 12
অ্যালগরিদম
ধাপ ১ - শুরু ধাপ ২ - দুটি পূর্ণসংখ্যা ঘোষণা করুন: vowels_count, consonants_count এবং একটি স্ট্রিং: my_str ধাপ ৩ - ব্যবহারকারীকে একটি স্ট্রিং ইনপুট দিতে বলুন / স্ট্রিং সংজ্ঞায়িত করুন ধাপ ৪ - মানগুলো পড়ুন ধাপ ৫ - একটি for-লুপ চালান, প্রতিটি অক্ষর চেক করুন যে সেটি স্বরবর্ণ কিনা ব্যঞ্জনবর্ণ। সংশ্লিষ্ট কাউন্টার বাড়ান। মান সংরক্ষণ করুন ধাপ ৬ - ফলাফল প্রদর্শন করুন ধাপ ৭ - শেষ
উদাহরণ ১
এখানে ব্যবহারকারী প্রম্পট অনুযায়ী ইনপুট দিচ্ছেন। আপনি আমাদের কোডিং গ্রাউন্ড টুলে এই উদাহরণটি লাইভ চেষ্টা করতে পারেন
।
import java.util.Scanner;
public class VowelAndConsonants {
public static void main(String[] args) {
int vowels_count, consonants_count;
String my_str;
vowels_count = 0;
consonants_count = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("A scanner object has been defined ");
System.out.print("Enter a statement: ");
my_str = scanner.nextLine();
my_str = my_str.toLowerCase();
for (int i = 0; i < my_str.length(); ++i) {
char ch = my_str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
++vowels_count;
}
else if ((ch >= 'a' && ch <= 'z')) {
++consonants_count;
}
}
System.out.println("The number of vowels in the statement is: " + vowels_count);
System.out.println("The number of consonants in the statement is: " + consonants_count);
}
}
আউটপুট
A scanner object has been defined Enter a statement: Hello, my name is Charlie The number of vowels in the statement is: 8 The number of consonants in the statement is: 12
উদাহরণ ২
এখানে মানটি পূর্বে সংজ্ঞায়িত করা হয়েছে, এবং কনসোলে অ্যাক্সেস করে প্রদর্শন করা হয়েছে।
public class VowelAndConsonants {
public static void main(String[] args) {
int vowels_count, consonants_count;
vowels_count = 0;
consonants_count = 0;
String my_str = "Hello, my name is Charlie";
System.out.println("The statement is defined as : " + my_str);
my_str = my_str.toLowerCase();
for (int i = 0; i < my_str.length(); ++i) {
char ch = my_str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
++vowels_count;
}
else if ((ch >= 'a' && ch <= 'z')) {
++consonants_count;
}
}
System.out.println("The number of vowels in the statement is: " + vowels_count);
System.out.println("The number of consonants in the statement is: " + consonants_count);
}
}
আউটপুট
The statement is defined as : Hello, my name is Charlie The number of vowels in the statement is: 8 The number of consonants in the statement is: 11