Skip to content
Related Articles

Related Articles

Improve Article
Program to find the largest and smallest ASCII valued characters in a string
  • Difficulty Level : Easy
  • Last Updated : 20 May, 2021

Given a string of lower case 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’. 
 

C++




// C++ program to find largest and smallest
// characters in a string.
#include <iostream>
using namespace std;
 
// function that return the largest alphabet.
char largest_alphabet(char a[], int n)
{
    // initializing max alphabet to 'a'
    char max = 'A';
 
    // find largest alphabet
    for (int i=0; i<n; i++)   
        if (a[i] > max)
            max = a[i];   
 
    // returning largest element
    return max;
}
 
// function that return the smallest alphabet
char smallest_alphabet(char a[], int n)
{
    // initializing smallest alphabet to 'z'
    char min = 'z';
 
    // find smallest alphabet
    for (int i=0; i<n-1; i++)   
        if (a[i] < min)
            min = a[i];   
 
    // returning smallest alphabet
    return min;
}
 
// Driver Code
int main()
{
    // Character array
    char a[]= "GeEksforGeeks";
 
    // Calculating size of the string
    int size = sizeof(a) / sizeof(a[0]);
 
    // calling functions and print returned value
    cout << "Largest and smallest alphabet is : ";
 
    cout << largest_alphabet(a,size)<< " and ";
    cout << smallest_alphabet(a,size)<<endl;
    return 0;
}

Java




// Java program to find largest and 
// smallest characters in a string.
public class GFG {    
     
    // function that return the largest alphabet.
    static char largest_alphabet(String a, int n)
    {
        // initializing max alphabet to 'a'
        char max = 'A';
      
        // find largest alphabet
        for (int i=0; i<n; i++)   
            if (a.charAt(i) > max)
                max = a.charAt(i);   
      
        // returning largest element
        return max;
    }
      
    // function that return the smallest alphabet
    static char smallest_alphabet(String a, int n)
    {
        // initializing smallest alphabet to 'z'
        char min = 'z';
      
        // find smallest alphabet
        for (int i=0; i<n-1; i++)   
            if (a.charAt(i) < min)
                min = a.charAt(i);   
      
        // returning smallest alphabet
        return min;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        // Input String
        String a= "GeEksforGeeks";
      
        // Calculating size of the string
        int size = a.length();
      
        // calling functions and print returned value
        System.out.print("Largest and smallest alphabet is : ");
      
        System.out.print(largest_alphabet(a,size) + " and ");
        System.out.println(smallest_alphabet(a,size));
    }
}
// This code is contributed by Sumit Ghosh

Python3




# Python3 program to find largest and 
# smallest characters in a string
 
# Function that return the largest alphabet
def largest_alphabet(a, n) :
     
    # Initializing max alphabet to 'a'
    max = 'A'
 
    # Find largest alphabet
    for i in range(n) :
        if (a[i] > max):
            max = a[i]
 
    # Returning largest element
    return max
 
# Function that return the smallest alphabet
def smallest_alphabet(a, n) :
     
    # Initializing smallest alphabet to 'z'
    min = 'z';
 
    # Find smallest alphabet
    for i in range (n - 1) :
        if (a[i] < min):
            min = a[i]
 
    # Returning smallest alphabet
    return min
     
# Driver code
if __name__ == '__main__' :
     
    # Character array
    a = "GeEksforGeeks"
     
    # Calculating size of the string
    size = len(a)
     
    # Calling functions and print returned value
    print( "Largest and smallest alphabet is : ", end = "")
     
    print(largest_alphabet(a, size), end = " and ")
    print(smallest_alphabet(a, size))
         
 
# This code is contributed by 'rishabh_jain'

C#




// C# program to find largest and
// smallest characters in a string.
using System;
 
class GFG {    
     
    // function that return the
    // largest alphabet.
    static char largest_alphabet(String a,
                                 int n)
    {
         
        // initializing max
        // alphabet to 'a'
        char max = 'A';
     
        // find largest alphabet
        for (int i = 0; i < n; i++)
            if (a[i] > max)
                max = a[i];
     
        // returning largest element
        return max;
    }
     
    // function that return the
    // smallest alphabet
    static char smallest_alphabet(String a,
                                  int n)
    {
         
        // initializing smallest
        // alphabet to 'z'
        char min = 'z';
     
        // find smallest alphabet
        for (int i = 0; i < n - 1; i++)
            if (a[i] < min)
                min = a[i];
     
        // returning smallest alphabet
        return min;
    }
     
    // Driver Code
    public static void Main()
    {
        // Input String
        String a= "GeEksforGeeks";
     
        // Calculating size
        // of the string
        int size = a.Length;
     
        // calling functions and
        // print returned value
        Console.Write("Largest and smallest alphabet is : ");
         
        Console.Write(largest_alphabet(a, size) + " and ");
        Console.Write(smallest_alphabet(a, size));
    }
}
 
// This code is contributed by nitin mittal.

php




<?php
// php program to find largest and smallest
// characters in a string.
 
// function that return the largest alphabet.
function largest_alphabet($a, $n)
{
    // Initializing max alphabet to 'a'
    $max = 'A';
 
    // Find largest alphabet
    for ($i = 0; $i < $n; $i++)
        if ($a[$i] > $max)
            $max = $a[$i];
 
    // Returning largest element
    return $max;
}
 
// function that return the smallest alphabet
function smallest_alphabet($a, $n)
{
    // initializing smallest alphabet to 'z'
    $min = 'z';
 
    // find smallest alphabet
    for ($i = 0; $i < $n-1; $i++)
        if ($a[$i] < $min)
            $min = $a[$i];
 
    // returning smallest alphabet
    return $min;
}
 
// Driver Code
 
    // Character array
    $a = "GeEksforGeeks";
 
    // Calculating size of the string
    $size = strlen($a);
 
    // calling functions and print returned value
    echo "Largest and smallest alphabet is : ";
 
    echo largest_alphabet($a, $size). " and ";
    echo smallest_alphabet($a, $size);
     
// This code is contributed by Sam007
?>

Javascript




<script>
 
    // JavaScript program to find largest and
    // smallest characters in a string.
     
    // function that return the
    // largest alphabet.
    function largest_alphabet(a, n)
    {
           
        // initializing max
        // alphabet to 'a'
        let max = 'A';
       
        // find largest alphabet
        for (let i = 0; i < n; i++)
            if (a[i].charCodeAt() > max.charCodeAt())
                max = a[i];
       
        // returning largest element
        return max;
    }
       
    // function that return the
    // smallest alphabet
    function smallest_alphabet(a, n)
    {
           
        // initializing smallest
        // alphabet to 'z'
        let min = 'z';
       
        // find smallest alphabet
        for (let i = 0; i < n - 1; i++)
            if (a[i].charCodeAt() < min.charCodeAt())
                min = a[i];
       
        // returning smallest alphabet
        return min;
    }
     
    // Input String
    let a= "GeEksforGeeks";
 
    // Calculating size
    // of the string
    let size = a.length;
 
    // calling functions and
    // print returned value
    document.write("Largest and smallest alphabet is : ");
 
    document.write(largest_alphabet(a, size) + " and ");
    document.write(smallest_alphabet(a, size));
     
</script>

Output:  

Largest and smallest alphabet is : s and E

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :