Program to find the largest and smallest ASCII valued characters in a string
Last Updated :
08 Feb, 2024
Given a string of lowercase and uppercase characters, your task is to find the largest and smallest alphabet (according to ASCII values) in the string. Note that in ASCII, all capital letters come before all small letters.
Examples :
Input : sample string
Output : Largest = t, Smallest = a
Input : Geeks
Output : Largest = s, Smallest = G
Explanation: According to alphabetical order
largest alphabet in this string is 's'
and smallest alphabet in this string is
'G'( not 'e' according to the ASCII value)
Input : geEks
Output : Largest = s, Smallest = E
The maximum possible value can be ‘z’ and smallest possible value can be ‘A’.
Implementation:
C++
#include <iostream>
using namespace std;
char largest_alphabet( char a[], int n)
{
char max = 'A' ;
for ( int i=0; i<n; i++)
if (a[i] > max)
max = a[i];
return max;
}
char smallest_alphabet( char a[], int n)
{
char min = 'z' ;
for ( int i=0; i<n-1; i++)
if (a[i] < min)
min = a[i];
return min;
}
int main()
{
char a[]= "GeEksforGeeks" ;
int size = sizeof (a) / sizeof (a[0]);
cout << "Largest and smallest alphabet is : " ;
cout << largest_alphabet(a,size)<< " and " ;
cout << smallest_alphabet(a,size)<<endl;
return 0;
}
|
Java
public class GFG {
static char largest_alphabet(String a, int n)
{
char max = 'A' ;
for ( int i= 0 ; i<n; i++)
if (a.charAt(i) > max)
max = a.charAt(i);
return max;
}
static char smallest_alphabet(String a, int n)
{
char min = 'z' ;
for ( int i= 0 ; i<n- 1 ; i++)
if (a.charAt(i) < min)
min = a.charAt(i);
return min;
}
public static void main(String args[])
{
String a= "GeEksforGeeks" ;
int size = a.length();
System.out.print( "Largest and smallest alphabet is : " );
System.out.print(largest_alphabet(a,size) + " and " );
System.out.println(smallest_alphabet(a,size));
}
}
|
Python3
def largest_alphabet(a, n) :
max = 'A'
for i in range (n) :
if (a[i] > max ):
max = a[i]
return max
def smallest_alphabet(a, n) :
min = 'z' ;
for i in range (n - 1 ) :
if (a[i] < min ):
min = a[i]
return min
if __name__ = = '__main__' :
a = "GeEksforGeeks"
size = len (a)
print ( "Largest and smallest alphabet is : " , end = "")
print (largest_alphabet(a, size), end = " and " )
print (smallest_alphabet(a, size))
|
C#
using System;
class GFG {
static char largest_alphabet(String a,
int n)
{
char max = 'A' ;
for ( int i = 0; i < n; i++)
if (a[i] > max)
max = a[i];
return max;
}
static char smallest_alphabet(String a,
int n)
{
char min = 'z' ;
for ( int i = 0; i < n - 1; i++)
if (a[i] < min)
min = a[i];
return min;
}
public static void Main()
{
String a= "GeEksforGeeks" ;
int size = a.Length;
Console.Write( "Largest and smallest alphabet is : " );
Console.Write(largest_alphabet(a, size) + " and " );
Console.Write(smallest_alphabet(a, size));
}
}
|
Javascript
<script>
function largest_alphabet(a, n)
{
let max = 'A' ;
for (let i = 0; i < n; i++)
if (a[i].charCodeAt() > max.charCodeAt())
max = a[i];
return max;
}
function smallest_alphabet(a, n)
{
let min = 'z' ;
for (let i = 0; i < n - 1; i++)
if (a[i].charCodeAt() < min.charCodeAt())
min = a[i];
return min;
}
let a= "GeEksforGeeks" ;
let size = a.length;
document.write( "Largest and smallest alphabet is : " );
document.write(largest_alphabet(a, size) + " and " );
document.write(smallest_alphabet(a, size));
</script>
|
php
<?php
function largest_alphabet( $a , $n )
{
$max = 'A' ;
for ( $i = 0; $i < $n ; $i ++)
if ( $a [ $i ] > $max )
$max = $a [ $i ];
return $max ;
}
function smallest_alphabet( $a , $n )
{
$min = 'z' ;
for ( $i = 0; $i < $n -1; $i ++)
if ( $a [ $i ] < $min )
$min = $a [ $i ];
return $min ;
}
$a = "GeEksforGeeks" ;
$size = strlen ( $a );
echo "Largest and smallest alphabet is : " ;
echo largest_alphabet( $a , $size ). " and " ;
echo smallest_alphabet( $a , $size );
?>
|
Output
Largest and smallest alphabet is : s and E
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...