Write a method in java named secondHalfLetters that accepts a string as its para
ID: 3554377 • Letter: W
Question
Write a method in java named secondHalfLetters that accepts a string as its parameter and returns an integer representing how many of letters in the string come from the second half of the alphabet (that is, have values of 'n' through 'z' inclusive). Compare case-insensitively, such that uppercase values of 'N' through 'Z' also count. For example, the call secondHalfLetters("ruminates") should return 5 because the 'r', 'u', 'n', 't', and 's' come from the second half of the alphabet. You may assume that every character in the string is a letter.
Explanation / Answer
public static int secondHalfLetters( String s)
{
s = s.toLowerCase() ;
int res = 0 ;
for(int i = 0 ; i < s.length() ; i++ )
if( s.charAt(i) >= 'n' && s.charAt(i) <= 'z' )
res++ ;
return res ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.