Java Program to Find the First Capital Letter/Small Letter/Vowel/Consonant/Number/White Space/Special Character in a Given String – Sanfoundry

This is the Java Program to Find the First Capital Letter/Small Letter/Vowel/Consonant/Number/White Space/Special Character in a Given String.

Problem Description

Given a string, find the first position of the capital letter, small letter, vowel, consonant, whitespace, and special character if present in the string.
Example:

String x = “Ac @”

Output =
Capital letter = 1
Small letter = 2
Vowel = 1
Consonant = 2
Whitespace = 3
Special Character = 4

Problem Solution

Iterate through the string and find the first positions.

Program/Source Code

Here is the source code of the Java Program to Find the First Capital Letter/Small Letter/Vowel/Consonant/Number/White Space/Special Character in a Given String. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. // Java Program to Find the First Capital Letter/Small Letter/Vowel
  3. // Consonant/Number/White Space/Special Character in a Given String
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7.  
  8. public class FirstIndexes {
  9.     // Function to display the output
  10.     static void firstIndexes(String str){
  11.         boolean consonant,vowel,capital,small,special,number,space;
  12.         consonant = vowel = capital = small = special = number = space =false;
  13.         int i;
  14.         char ch;
  15.         int count = 0;
  16.         for(i=0; i<str.length(); i++){
  17.             if(count == 7)
  18.                 break;
  19.             ch = str.charAt(i);
  20.             if(!space && ch == ' '){
  21.                 System.out.println("The index of first whitespace is " + (i+1));
  22.                 space = true;
  23.                 count++;
  24.             }
  25.             else if(!number && Character.isDigit(ch)){
  26.                 System.out.println("The index of first number is " + (i+1));
  27.                 number = true;
  28.                 count++;
  29.             }
  30.             if(Character.isLetter(ch)){
  31.                 if(!small && Character.isLowerCase(ch)){
  32.                     System.out.println("The index of first lowercase character is "
  33.                                                                           + (i+1));
  34.                     small = true;
  35.                     count++;
  36.                 }
  37.                 else if(!capital && Character.isUpperCase(ch)){
  38.                     System.out.println("The index of first uppercase character is "
  39.                                                                           + (i+1));
  40.                     capital = true;
  41.                     count++;
  42.                 }
  43.                 if(!vowel || !consonant){
  44.                     ch = Character.toLowerCase(ch);
  45.                     if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
  46.                                                            || ch == 'u'){
  47.                         System.out.println("The index of first vowel is "
  48.                                                                  + (i+1));
  49.                         vowel = true;
  50.                         count++;
  51.                     }
  52.                     else{
  53.                         System.out.println("The index of first consonant is "
  54.                                                                       + (i+1));
  55.                         consonant = true;
  56.                         count++;
  57.                     }
  58.                 }
  59.             }
  60.             else if(!special && ch != ' '){
  61.                 System.out.println("The index of first special character is "
  62.                                                                      + (i+1));
  63.                 special = true;
  64.                 count++;
  65.             }
  66.         }
  67.     }
  68.     // Function to read the string
  69.     public static void main(String[] args) {
  70.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  71.         String str;
  72.         System.out.println("Enter the string");
  73.         try {
  74.             str = br.readLine();
  75.         } catch (Exception e) {
  76.             System.out.println("An I/O error occurred");
  77.             return;
  78.         }
  79.         System.out.println("The first indexes are");
  80.         firstIndexes(str);
  81.     }
  82. }

Program Explanation

1. In function firstIndexes(), several boolean variables consonant,vowel,capital,small,special,number and space are created and initialized to false.
2. The loop for(i=0; i<str.length(); i++) is used to iterate through the string.
3. The condition if(count == 7) checks whether each type of character is found, if is true, break from the loop.
4. The condition if(!space && ch == ‘ ‘) checks if the given character is a whitespace and displays it, if it is.
5. Similarly, the other else-if conditions check for remaining types of characters and display them.

Time Complexity: O(n) where n is the length of the string.

Runtime Test Cases

 
Case 1 (Simple Test Case):
 
Enter the string
Ac @
The first indexes are
The index of first uppercase character is 1
The index of first vowel is 1
The index of first lowercase character is 2
The index of first consonant is 2
The index of first whitespace is 3
The index of first special character is 4
 
Case 2 (Simple Test Case - another example):
 
Enter the string
@Timbuchalka
The first indexes are
The index of first special character is 1
The index of first uppercase character is 2
The index of first consonant is 2
The index of first lowercase character is 3
The index of first vowel is 3

Sanfoundry Global Education & Learning Series – Java Programs.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Leave a Comment

Your email address will not be published. Required fields are marked *