0% found this document useful (0 votes)
7 views6 pages

Count Vowels and Consonants in String

Uploaded by

padma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Count Vowels and Consonants in String

Uploaded by

padma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

print vowels from string?

count how many vowels in string?


count vowels and consonant in string?

a e i o u

String a = "jenkins";

int vowelCount = 0; //2


int consonantsCount = 0

for(char value: [Link]())


{

if(value == 'a' || value == 'e' || value=='i' || value=='o'


|| value=='u' )
{
vowelCount = vowelCount +1;
[Link](value);
}
else
{
consonantsCount = consonantsCount +1;
}
}

[Link](value);
[Link](consonantsCount);

package tutorial19;

public class Demo1 {

public static void main(String[] args) {

String a = "jenkins";

int vowelCount = 0;

int consonantCount = 0;

String vowel="";

String consonant="";

for(char value: [Link]())


{

if(value == 'a' || value=='e' || value =='i' || value=='o' ||


value =='u')
{
vowelCount++;
vowel = vowel +value;
}
else
{
consonantCount++;
consonant = consonant +value;

[Link](vowel +"::"+vowelCount); //2

[Link](consonant +"::"+ consonantCount); // 5

}
===================================================================================
=========
package tutorial19;

import [Link];
import [Link];

public class Demo1 {

public static void main(String[] args) {

String a = "jenkins";

int vowelCount = 0;

int consonantCount = 0;

String vowel="";

String consonant="";

for(char value: [Link]())


{

if(value == 'a' || value=='e' || value =='i' || value=='o' ||


value =='u')
{
vowelCount++;
vowel = vowel +value;
}
else
{
consonantCount++;
consonant = consonant +value;

[Link](vowel +"::"+vowelCount); //2

[Link](consonant +"::"+ consonantCount); // 5

}
===================================================================================
======
count length of string without using length() method

String a = "java";

int count = 0;//4

for(char value: [Link]())


{
count = count +1;
}

[Link](count);

package tutorial19;

public class Demo2 {

public static void main(String[] args) {

String a = "selenium";

int count = 0;

for(char value: [Link]())


{
count = count +1;
}

[Link](count);
}

}
=======================================================================
find longest words string from sentence

String a ="java selenium automation rest api";

String longest = "automation"; //10

String [] abc =[Link](" ");

for(String value: abc)


{

if([Link]() > [Link]())


{
longest = value;
}

[Link](longest);

package tutorial19;

public class Demo3 {

public static void main(String[] args) {

String a ="java selenium restassured automation api";

String longest = "";//

String [] abc = [Link](" ");

for(String value: abc)


{

if([Link]() > [Link]())


{

longest = value;

[Link](longest);//restassured

}
}
=========================================================================
package tutorial19;

public class Demo3 {

public static void main(String[] args) {

String a ="java selenium automation Automation api";

String longest = "";//

String [] abc = [Link](" ");

for(String value: abc)


{

if([Link]() >= [Link]())


{

longest = value;

[Link](longest);//restassured

}
===================================================================================
==========

Common questions

Powered by AI

The program maintains separate variables for vowels and consonants. It iterates over each character in the string, appending vowels to one string and consonants to another, using a conditional statement to discriminate. It then prints the concatenated vowel and consonant strings along with their counts by the end of the iteration .

The methodology involves converting the string into a character array with toCharArray() and iterating through the array using a for-loop, merely counting the iterations to determine the string's length .

The program iterates over each character of the string using a for-loop, checking if it matches any of the vowels ('a', 'e', 'i', 'o', 'u'). If it does, it increments the vowel count; otherwise, it increments the consonant count .

Yes, the methods can be generalized for tasks such as text analysis, word frequency counting, and pattern recognition. The principles of iteration, conditional checks, and efficient data structures like arrays and hash-based collections can be applied to analyze any sequential data .

The program could be enhanced by using a HashSet data structure to store vowels for O(1) lookup time and recalibrating the logic to avoid concatenation within the loop, as string concatenation creates new string objects which can be costly. StringBuilder could be employed for more efficient string modifications .

The document details converting the string into a character array, then using a for-loop to iterate, counting specific characters. This approach is straightforward but can be inefficient for large strings due to character array conversion and multiple memory allocations. An alternative could be using a direct iteration on the string itself, potentially reducing memory overhead .

Differentiating between vowels and consonants aids in linguistic analysis and natural language processing tasks. It can improve readability scoring, assist in phonetic analysis, and serve in crafting learning tools for language education, where vowel-heavy words, for example, might affect syllabic structure and pronunciation guides .

The steps include splitting the sentence into individual words using the split method, iterating through these words using a for-loop, and comparing the length of each word to a longest variable. If a word is longer, it updates the longest variable, and at the end, it prints the longest word .

The program iterates through each word in the sentence using a for-loop. It maintains a longest variable initialized as an empty string. Whenever a word with a length equal to or greater than the current longest is encountered, it updates the longest variable, thus correctly handling ties by preferring subsequent tied occurrences .

The program uses a for-loop to iterate over each character, checking with conditional logic if the char is not a vowel and thus deducing it as a consonant. It then appends to the consonant string and increments the consonant count accordingly, ensuring correct categorization of each character .

You might also like