How To Find And Display Vowels In Given String With Its Current Position In Java.

Find and display vowels in given string with its current position in java

In this java programs we will see how to find vowels in given string (String entered form the user) with its current position. For that we will use four string methods :

1. toLowerCase()
2. charAt()
3. indexOf()
4. Length()

Description of above java four string methods with example :

1. toLowerCase() : This method converts a string to lower case letters.

Example :

public class Main {
  public static void main(String[] args) {
    String txt = "Hello World";
    System.out.println(txt.toLowerCase());
  }
}

Output:

hello world

2. charAt() : This method is used to find the character associated with a certain position in a string. It can also return multiple characters in a string.

Example :

public class Main {
  public static void main(String[] args) {
    String myStr = "Hello welcome to java industry";
    char result = myStr.charAt(0);
    System.out.println(result);
  }
}

Output:

H

3. indexOf()() : This method returns the position of the first occurrence of specified character(s) in a string.

Example :

public class Main {
  public static void main(String[] args) {
    String myStr = "Hello good afternoon eveyone.";
    System.out.println(myStr.indexOf("e", 5));
  }
}

Output:

14

4. indexOf()() : This method returns the number of characters present in the string including spaces into 2 words.

Example :

public class Main {
  public static void main(String[] args) {
    String txt = "Hello good afternoon eveyone";
    System.out.println(txt.length());
  }
}

Output:

28

Now after learning the above four Java strings we will see our main purpose how to find the vowels with their current position in a given string.

Source Code:

package gsh.java.programs;
import java.util.Scanner;
public class GSH {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Any String ::");
        String str = sc.nextLine();
        String newStr = str.toLowerCase();
        int vowelsCount = 0;
        for (int i=0;i<newStr.length();i++){
            if(newStr.charAt(i)=='a'||newStr.charAt(i)=='e'||newStr.charAt(i)=='i'||newStr.charAt(i)=='o'||newStr.charAt(i)=='u'){
                System.out.println("Vowel Are : "+newStr.charAt(i)+" And There Position is : "+newStr.indexOf(newStr.charAt(i),i));
                vowelsCount++;
            }
        }
        System.out.println("Total Vowels Are : "+vowelsCount);
    }
}

Output:

Enter Any String ::
welcome to java industry
 
Vowel Are : e And There Position is : 1
Vowel Are : o And There Position is : 4
Vowel Are : e And There Position is : 6
Vowel Are : o And There Position is : 9
Vowel Are : a And There Position is : 12
Vowel Are : a And There Position is : 14
Vowel Are : i And There Position is : 16
Vowel Are : u And There Position is : 19
Total Vowels Are : 8

Post a Comment

0 Comments