Java program to find the first letter alphabetically in String

Posted by

Code:

 import java.util.Scanner;  
 class AlphaString {  
   public static void main(String args[]) {  
     int first = 0;  
     System.out.println("Please enter a word - ");  
     Scanner in = new Scanner(System.in);  
     String word = in.nextLine();  
     System.out.println("The word you entered was: " + word);  
     char[] wordArr=word.toLowerCase().toCharArray();  
     for (int i = 1; i < wordArr.length; i++) {  
       if(Character.isWhitespace(wordArr[i])){  
       continue;  
       }  
       else{  
         if (wordArr[i] < wordArr[first]) {  
         first = i;  
       }  
       }  
     }  
     System.out.println("First letter of Entered string: "+word.charAt(first));  
   }  
 }  

Output:

Java program to find the first letter alphabetically in String

 

 

 

 

2 comments

  1. I want to sharpen my skills in java programming so I like paying visit to a Java forum of my choice, so that I may learn various tricks and tactics. Now, I am going to follow this blog as well, as it will help enriching my knowledge.

Leave a Reply