Advertisements
Advertisements
प्रश्न
A linked list is formed from the objects of the class Word. The structure of the class Word is given below:
class Word
{
String value;
Word next;
}
Write an Algorithm OR a Method to count and display the number of nodes whose value starts with a consonant.
The method declaration is as follows:
void countConsonant(Word first)
कोड लेखन
Advertisements
उत्तर
void countConsonant(Word first) {
int count = 0;
Word ptr = first; // Pointer to traverse the list
while (ptr != null) {
String s = ptr.value;
// Ensure string is not null and not empty
if (s != null && s.length() > 0) {
char ch = Character.toLowerCase(s.charAt(0));
// Check if it's a letter and not a vowel
if (ch >= 'a' && ch <= 'z') {
if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') {
count++;
}
}
}
ptr = ptr.next; // Move to the next node
}
System.out.println("Number of nodes starting with a consonant: " + count);
}shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2025-2026 (March) Official Board Paper
