chabztar01
chabztar01

Reputation: 61

Write programs that read a line of input as a string and print the positions of all vowels in the string

I'm a beginner in Java and I have a question regarding loops. I've been struggling with this task where it says: Write programs that read a line of input as a string and print the positions of all vowels in the string.

I have managed to print out the number of vowels in the input but I got stuck when it came to print out their positions.

Any help would be appreciated!

System.out.println("Enter your input: ");
    String input = in.nextLine();

    int sum = 0;

    for(int i = 0; i < input.length(); i++){
        char vowel = input.charAt(i);
        if(vowel == 'a'|| vowel == 'e'|| vowel == 'i'||
                vowel == 'o'|| vowel == 'u'|| vowel == 'y'){
            sum++;
        }

    }
    System.out.println(sum);

Upvotes: 1

Views: 1371

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338730

Avoid char

The char type has been essentially broken since Java 2, and legacy since Java 5. As a 16-bit value, char is physically incapable of representing most of the >144,000 characters defined in Unicode.

Code point

To work with individual characters, use code point integer numbers.

Define your set of targeted vowels. Sort the array, so that we may search quickly via binary search.

int[] vowelCodePoints = "aeiouy".codePoints().sorted().toArray() ;

Get the code points of the characters in your input.

int[] codePoints = "test".codePoints().toArray() ;

Loop the array of code points of our input string, by index (zero-based counting). Test each code point to see if it is found within our array of vowel code points.

To go in the reverse direction, from code point integer to the character as text, call Character.toString.

Example code

Pull all the code together.

int[] vowelCodePoints = "aeiouy".codePoints().sorted().toArray();

int[] codePoints = "testy".codePoints().toArray();
List < Integer > indicesOfVowels = new ArrayList <>( codePoints.length );
for ( int index = 0 ; index < codePoints.length ; index++ )
{
    int position = Arrays.binarySearch( vowelCodePoints , codePoints[ index ] );
    if ( position >= 0 )
    {
        String vowel = Character.toString( codePoints[ index ] );
        System.out.println( "Vowel: " + vowel + " | Index: " + index );
    }
}

See this code run at Ideone.com.

Vowel: e | Index: 1
Vowel: y | Index: 4

Upvotes: 0

Deviprasad Sharma
Deviprasad Sharma

Reputation: 536

System.out.println("Enter your input: ");
    String input = in.nextLine();

    int sum = 0;

    for(int i = 0; i < input.length(); i++){
        char vowel = input.charAt(i);
        if(vowel == 'a'|| vowel == 'e'|| vowel == 'i'||
                vowel == 'o'|| vowel == 'u'|| vowel == 'y'){
            sum++;
            System.out.println("position ->"+i);//added line
        }

    }
    System.out.println(sum);

you just needed to add single line that prints the positions

Upvotes: 4

Related Questions