What does the following program do? (What is its purpose / functionality?) Be sp
ID: 3802716 • Letter: W
Question
What does the following program do? (What is its purpose / functionality?) Be specific.
public int countU( String str )
{
int maxLength = str.length();
int countU = 0;
int asciiCode;
for(int i = 0; i < maxLength; i++) {
asciiCode = (int)str.charAt(i);
if( asciiCode >= 65 && asciiCode <= 90 ) {
countU++;
}
}
return countU;
}
Explanation / Answer
Solution:
Best way is to explain each line one by one;
public int countU( String str ) // This is a function which will receive string as an argument
{
int maxLength = str.length(); //Here legth of the received string is saved in maxLength
int countU = 0; //countU is intialized to 0 here
int asciiCode; // a variable named asciiCode is declared here
for(int i = 0; i < maxLength; i++) { // the loop is running maxLength number of times means string length
asciiCode = (int)str.charAt(i); //ascii value of the charcter of string will be saved here in variable //asciiCode one by one.
if( asciiCode >= 65 && asciiCode <= 90 ) { // Since ascii value from 65 to 90 is compared here and ascii value of 65 to 90 are A to Z all in upper case letters are there this means that only Upper case letters in the string will satisfy this condition
countU++; // Number of upper case letters in the string will be counted here
}
}
return countU; // The value of countU will be returned, which will be number of upper case letters available in the string.
}
I hope this helps. Don't forget to give a thumbs up if you like this.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.