WAP to Print Highest ASCII Value Character From String

Print Highest ASCII Value Character From String: This Java program takes an input string and finds the character with the largest ASCII value in that string. It achieves this by iterating through each character of the input string and keeping track of the greatest ASCII value encountered so far. The program then returns the character corresponding to the greatest ASCII value as the result.

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms31 
{
	/*
	 * Input string s ="amZgxY" 
	 * Need to print biggest character asiic value.
	 */
	public static void main(String[] args) 
	{
		String str = "amZgxY";
		System.out.println(test(str));
	}
	public static char test ( String str ) 
	{
		int greatestVal = 0 ;
		int len = str.length();
		for (int i = 0 ; i<len; i++ )
		{
			int currentVal = (int)str.charAt(i);
			if (currentVal>greatestVal)
			{
				greatestVal=currentVal ;
			}
		}
		char greatestChar = (char)(greatestVal);
		return greatestChar ;
	}
}

Output

x

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize variables and process the input string:
    • The input string str is initialized with the value “amZgxY”.
    • The program calls the test method, passing the input string as an argument.
  3. The test method:
    • The test method takes a string str as input and returns a character with the largest ASCII value.
    • It initializes greatestVal to 0 to keep track of the greatest ASCII value encountered so far.
    • It calculates the length of the input string and iterates through each character using a for loop.
    • For each character, it converts it to its ASCII value using (int)str.charAt(i) and stores it in currentVal.
    • If currentVal is greater than greatestVal, it updates greatestVal to currentVal.
    • After processing all characters, it converts greatestVal back to a character using (char)(greatestVal) and stores it in greatestChar.
    • The method then returns greatestChar.
  4. Print the result:
    • The program prints the character returned by the test method, which is the character with the largest ASCII value in the input string.

Overall, this program finds and prints the character with the largest ASCII value in the input string “amZgxY,” which is ‘x’ (with ASCII value 120). The program demonstrates basic string manipulation and character conversion in Java.

Alternative Way 1:

This Java program takes an input string and finds the character with the largest ASCII value in that string. It achieves this by iterating through each character of the input string using an enhanced for loop and keeping track of the greatest ASCII value encountered so far. The program then prints the character corresponding to the greatest ASCII value as the result.

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms31_1 
{
	/*
	 * Input string s ="amZgxY" 
	 * Need to print biggest character asiic value.
	 */
	public static void main(String[] args) 
	{
		String str = "amZgxY";
		int currentmax = 0 ;
		for (Character c : str.toCharArray()) 
		{
			if ((int)c>currentmax)
				currentmax = (int)c;
		}
		System.out.println ((char)currentmax);
	}
}

Output

x

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize variables and process the input string:
    • The input string str is initialized with the value “amZgxY”.
    • An integer currentmax is initialized to 0 to keep track of the greatest ASCII value encountered so far.
  3. Loop through each character:
    • The program iterates through each character of the input string str using an enhanced for loop.
    • For each character, the program checks if its ASCII value (casted to an integer using (int)c) is greater than the current maximum (currentmax).
    • If the ASCII value of the current character is greater than currentmax, it updates currentmax to the ASCII value of the current character.
  4. Print the result:
    • After processing all characters, the program prints the character corresponding to the currentmax ASCII value using (char)currentmax. This is the character with the largest ASCII value in the input string.

Overall, this program achieves the same functionality as the previous one by finding and printing the character with the largest ASCII value in the input string “amZgxY,” which is ‘x’ (with ASCII value 120). The program demonstrates a more concise way to achieve the same result by using an enhanced for loop and directly iterating through characters without using an index.

Alternative Way 2:

This Java program finds and prints the character with the largest ASCII value in the input string “amZgxY” using three different approaches: using Java Stream API, using a traditional for loop, and using the Collections class.

package com.softwaretestingo.interviewprograms;
import java.util.Collections;
import java.util.stream.Collectors;
public class InterviewPrograms31_2 
{
	/*
	 * Input string s ="amZgxY" 
	 * Need to print biggest character asiic value.
	 */
	public static void main(String[] args) 
	{
		String str = "amZgxY";

		// Using Stream
		char c = (char)str.chars().max().getAsInt();
		System.out.println ( c ) ;


		// using for each loop
		int max = 0 ;
		for (char char_val : str.toCharArray())
			if ( char_val > max )
				max = char_val ;
		System.out.println ((char)max);


		// Using Collections class
		char c1 = (char)Collections.max(str.chars().boxed().collect(Collectors.toList())).intValue();
		System.out.println(c1);
	}
}

Output

x
x
x

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize the input string:
    • The input string str is initialized with the value “amZgxY”.
  3. Using Stream API:
    • The program uses Java Stream API to find the largest ASCII value character in the input string.
    • It converts the string into an IntStream of ASCII values using str.chars().
    • It finds the maximum ASCII value in the IntStream using max() and stores it as an integer using getAsInt().
    • The integer value is then cast back to a character c using (char) and printed.
  4. Using a for loop:
    • The program iterates through each character of the input string using a traditional for each loop.
    • It keeps track of the maximum ASCII value found so far in the variable max.
    • After processing all characters, it prints the character corresponding to the max ASCII value.
  5. Using Collections class:
    • The program uses the Collections class to find the largest ASCII value character in the input string.
    • It converts the string into a list of ASCII values using Stream API and Collectors.toList().
    • It finds the maximum ASCII value in the list using Collections.max() and stores it as an Integer.
    • The Integer value is then cast back to a character c1 using (char) and printed.

Overall, the program demonstrates three different approaches to finding the character with the largest ASCII value in the input string “amZgxY,” which is ‘x’ (with ASCII value 120). It shows how Java Stream API and the Collections class can be used to simplify such tasks.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment