101k views
1 vote
#Write a function called has_a_vowel. has_a_vowel should have #one parameter, a string. It should return True if the string #has any vowels in it, False if it does not.

User Kijana
by
6.6k points

1 Answer

5 votes

Answer:

Here is code in java.

import java.util.*;

class Main

{

// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

try{

// scanner object to read input from user

Scanner obj=new Scanner(System.in);

System.out.println("please enter a string:");

//read the string input

String inp=obj.next();

// call the function to check whether string has vowel or not

Boolean res=has_a_vowel(inp);

// printing the result

System.out.println("string contains vowel."+res);

}catch(Exception ex){

return;}

}

// method to check string has vowel or not

public static boolean has_a_vowel(String str)

{

// find length of the string

int len=str.length();

Boolean flag=false;

int i;

for( i=0;i<len;i++)

{

char chh=str.charAt(i);

// if string contains vowel then flag will be true

if(chh=='a'||chh=='e'||chh=='i'||chh=='o'||chh=='u')

{

flag=true;

break;

}

}

//returning the result

return flag;

}

}

Step-by-step explanation:

Create a scanner class object to read the string and assign it to variable

"inp".Call the function "has_a_vowel" with a string parameter.In this function

find the length of string and travers the string.If there is any vowel character

in the string then flag will be set to true otherwise flag will be false. Then it will

return the value of flag.We can test the function from the main by passing a string parameter.

Output:

please enter a string:

vjcavcavacvajjfd

string contains vowel:true

please enter a string:

sdfdhgjkhltyrqwzxcvbmm

string contains vowel: false

User Andrey Ermakov
by
5.6k points