write a method called luckyAs() that takes any array of strings and returns the
ID: 3939947 • Letter: W
Question
write a method called luckyAs() that takes any array of strings and returns the number of times the letter "A" appears among all the strings in the array. Fill in the method signature below, then implement the method.Lastly , provide a set of test cases (inputs and expected outputs)
public static ______ ______ (_____) write a method called luckyAs() that takes any array of strings and returns the number of times the letter "A" appears among all the strings in the array. Fill in the method signature below, then implement the method.
Lastly , provide a set of test cases (inputs and expected outputs)
public static ______ ______ (_____) write a method called luckyAs() that takes any array of strings and returns the number of times the letter "A" appears among all the strings in the array. Fill in the method signature below, then implement the method.
Lastly , provide a set of test cases (inputs and expected outputs)
public static ______ ______ (_____)
Explanation / Answer
// LuckyCharacter.java
// counts frequency of capital A in string Array
import java.util.Scanner;
public class LuckyCharacter
{
public static int LuckyAs(String[] str)
{
char ch;
char c ='A';
int count=0;
for(int i=0; i<str.length; i++)
{
for (int j = 0;j < str[i].length();j++ )
{
ch = str[i].charAt(j);
if(ch == c)
{
count++;
}
}
}
return count;
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int totalString = 3;
String[] str = new String[totalString];
str[0] = "Aloo";
str[1] = "BArAk";
str[2] = "BlAck Olives";
System.out.println("The character A has occurred for " + LuckyAs(str) + " times");
}
}
/*
output:
The character A has occurred for 4 times
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.