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

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

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: the Java Stream API, a traditional for loop, and 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

Avatar for Softwaretestingo Editorial Board

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