Find the alphabetically first character in a string

Updated

The program must accept a string value S contains only alphabets as the input. The program must print an alphabet from S which has occurred first in the alphabetical order as the output.

Examples :

Input : qwerty
Output : e
Explanation :
In the word qwerty, the character 'e' has occurred first in the
alphabetical order.Hence the output is e.

Input : zxsvwu
Output : s

Simple Approach :

Assume that the first character in the string is the alphabetically first character
Traverse through the string and replace the character which is alphabetically first
Print the character.

Implementation :

#include<stdio.h>
#include <string.h>

int main()
{
    char s[100];
    scanf("%s",s); 
    // assume the first character is alphabetically first
    char m = s[0];
    for(int i=1;i<strlen(s);i++)
    { 
        //if the current character is small then assume it as minimum
        if(s[i]<m)
            m = s[i];
    }
    printf("%c",m);
    return 0;
}
#print the first character in the sorted list of characters 
print(sorted(list(input().strip()))[0])
Comment
Mithlesh Upadhyay Published 12 Nov 2026 · 1 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials