package wordutilitpkg;

import java.util.*;

public class WordUtility
{
//returns true if the character c is a vowel and false otherwise. 
	public static boolean isVowel(char c)
	{
		if(c=='a' || c=='A' || 
		   c=='e' || c=='E' || 
		   c=='i' || c=='I' || 
		   c=='o' || c=='O' ||
		   c=='u' || c=='U') 
		{
			return true;
		}

		return false;
	}

//returns the position of the first vowel in s or -1 if s doesn't contain any vowels.
	public static int firstVowelPosition(String s)
	{
		int pos;
		for(pos = 0; pos < s.length(); pos++) {
			if(isVowel(s.charAt(pos)))
				return pos;
		}
		return -1;
	}

	//returns the position of the first blank character in s or -1 if it does not contain blanks
	public static int firstBlank(String s)
	{
		int pos;
		for(pos = 0; pos < s.length(); pos++) {
			if(s.charAt(pos) == ' ') {
				return pos;
			}
		}
		return -1;
	}

	public static String nthWord(String s, int n)
	{
		int i=0;
		String nthString;
		StringTokenizer tokenizer;

		tokenizer = new StringTokenizer(s);
		while(tokenizer.hasMoreTokens()) {
			nthString = tokenizer.nextToken();
			if(i==n) {
				return nthString;
			}
			i++;
		}
		return null;
	}
	
	//returns the first occurrence of c in s or -1. 
	public static int firstOccurrence(String s, char c)
	{
		int pos;
		for(pos=0; pos < s.length(); pos++) {
			if(s.charAt(pos) == c) {
				return pos;
			}
		}
		return -1;
	}

	//returns the position of the first nonblank character in s or -1 if s only contains blanks. 
	int firstNonBlank(String s)
	{
		int pos;
		for(pos = 0; pos < s.length(); pos++) {
			if(s.charAt(pos) != ' ') {
				return pos;
			}
		}
		return -1;
	}

	//returns the position of the first character in s that is not a vowel or -1 if s contains all vowels
	public static int firstNonVowel(String s)
	{
		int pos;
		for(pos = 0; pos < s.length(); pos++) {
			if(!isVowel(s.charAt(pos)))
				return pos;
		}
		return -1;
	}

	//returns the number of vowels in s.
	public static int numberVowels(String s)
	{
		int pos, vowels=0;
		for(pos=0; pos<s.length(); pos++) {
			if(isVowel(s.charAt(pos))) {
				vowels++;
			}
		}
		return -1;
	}

	//returns the number of non vowel characters in s.
	public static int numberNonVowels(String s)
	{
		int pos, nonVowels=0;
		for(pos = 0; pos < s.length(); pos++) {
			if(!isVowel(s.charAt(pos)))
				return nonVowels++;
		}
		return -1;
	}
}

