185k views
1 vote
maxCharacter * * a function to compute the 'maximum' character in the list using recursion * You will want to create a helper function to * do the recursion * * precondition: list is not empty * * Examples: * ["ababcdefb"].maxCharacter() == 'f' * ["eezzg"].maxCharacter() == 'z' * ["a"].maxCharacter() == 'a' */

1 Answer

1 vote

Answer:

import java.util.Scanner; //to take input from user

public class MaximumCharacter

{

char ch='A';//set initially A which is smallest char

//more than one arguments are required to find max character

char maxCharacter(String line, int len, int counter)

{

if(counter==len) //all characters are compared

return ch;

else

{

if(ch < line.charAt(counter)) //compare ch with each character of input

{

ch = line.charAt(counter); //update value of ch

}

counter++;//update counter to check next character

return maxCharacter(line, len, counter); //recursive call with new value of counter

}

}

public static void main(String arga[]) //main method to control the program

{

MaximumCharacter obj = new MaximumCharacter(); //crating object of class.

Scanner IN = new Scanner(System.in); //objct of scanner class to take input

System.out.print(System.lineSeparator() + "Enter a word to find max character: ");

String input = IN.nextLine(); //take user input

char c = obj.maxCharacter(input, input.length(),0); //call function to find max character

System.out.println("'" + input + "' Max Character = " + c); //print max character

}

}

Step-by-step explanation:

User Brentonk
by
5.1k points