This is the Java Program to Find the First Capital Letter/Small Letter/Vowel/Consonant/Number/White Space/Special Character in a Given String.
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
Iterate through the string and find the first positions.
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.
// Java Program to Find the First Capital Letter/Small Letter/Vowel
// Consonant/Number/White Space/Special Character in a Given String
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class FirstIndexes {
// Function to display the output
static void firstIndexes(String str){
boolean consonant,vowel,capital,small,special,number,space;
consonant = vowel = capital = small = special = number = space =false;
int i;
char ch;
int count = 0;
for(i=0; i<str.length(); i++){
if(count == 7)
break;
ch = str.charAt(i);
if(!space && ch == ' '){
System.out.println("The index of first whitespace is " + (i+1));
space = true;
count++;
}
else if(!number && Character.isDigit(ch)){
System.out.println("The index of first number is " + (i+1));
number = true;
count++;
}
if(Character.isLetter(ch)){
if(!small && Character.isLowerCase(ch)){
System.out.println("The index of first lowercase character is "
+ (i+1));
small = true;
count++;
}
else if(!capital && Character.isUpperCase(ch)){
System.out.println("The index of first uppercase character is "
+ (i+1));
capital = true;
count++;
}
if(!vowel || !consonant){
ch = Character.toLowerCase(ch);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u'){
System.out.println("The index of first vowel is "
+ (i+1));
vowel = true;
count++;
}
else{
System.out.println("The index of first consonant is "
+ (i+1));
consonant = true;
count++;
}
}
}
else if(!special && ch != ' '){
System.out.println("The index of first special character is "
+ (i+1));
special = true;
count++;
}
}
}
// Function to read the string
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter the string");
try {
str = br.readLine();
} catch (Exception e) {
System.out.println("An I/O error occurred");
return;
}
System.out.println("The first indexes are");
firstIndexes(str);
}
}
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.
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.
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Check Programming Books
- Check Java Books
- Practice BCA MCQs