2. Write a function that takes a single list of characters as an argument and re
ID: 3737191 • Letter: 2
Question
2. Write a function that takes a single list of characters as an argument and returns a new list of integers as a return value. The list returned must contain the ASCII codes of every uppercase character in the original list in the reverse order from the order they appeared in the original list. You are not permitted to use negative indexing (i.e., the numbers you write in the square brackets to retrieve a value from the list must be nonnegative), and you are not permitted to use any functions other than append, len, and ord. As a clarifying example, if your function was called with the argument ['H', 'e', 'l', 'L', 'o', 'W', 'o', 'r', ', 'D'] it must return the list [68, 87, 76,72] (because the ASCII codes for 'D', "W', 'L, 'H' are 68, 87, 76, 72, respectively)Explanation / Answer
int[] helper(char[] ch) //Array ch contains list of characters
{
int count=0; //Denotes the size of the resultant array containing ascii codes for uppercase characters
for(int i=0;i<ch.length;i++){
if(ch[i]>='A' && ch[i]<='Z'){
count++;
}
}
int[] arr=new int[count]; //Resultant array
int index=0;
for(int i=ch.length-1;i>=0;i--){
char get=ch[i];
if(get>='A' && get<='Z'){
arr[index]=(int)get;
index++;
}
}
return arr;
}
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.