You are given a String variable aStr that contains a String. Write a segment of
ID: 3631089 • Letter: Y
Question
You are given a String variable aStr that contains a String.Write a segment of Java code that will count the number of times a lowercase letter 'e' appears in aStr, and will print out the answer in exactly the form shown below. For example, if aStr contained the String "elementary" then your program would print
No. of little e's = 3
because aStr contained three occurrences of the letter 'e'.
Note: This is only an example. Your code must be general and must work correctly for any String aStr.
String aStr = scan.next();
Explanation / Answer
int countes(String aStr)
{
int count=0;
for(int i=0; i<aStr.lenth(); i++)
{
if(aStr.charAt(i) == 'e')
count++;
}
return count;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.