Computer Science, asked by royarijitaugust2005, 5 months ago

WAP in java to input a sentence and then print the words that carry even number of vowels.

Answers

Answered by shomekeyaroy79
1

\huge\mathtt\orange{AnSwEr}

Given a string str, write a Java program to print all words with even length in the given string.

Examples:

Input: s = "This is a java language"

Output: This

is

java

language

Input: s = "i am GFG"

Output: am

Approach:

Take the string

Break the string into words with the help of split() method in String class. It takes the string by which the sentence is to be broken. So here ” “(space) is passed as the parameter. As a result, the words of the string are split and returned as a string array

String[] words = str.split(" ");

Traverse each word in the string array returned with the help of Foreach loop in Java.

for(String word : words)

{ }

Calculate the length of each word using String.length() function.

int lengthOfWord = word.length();

If the length is even, then print the word.

Below is the implementation of the above approach:

// Java program to print

// even length words in a string

class GfG {

public static void printWords(String s)

{

// Splits Str into all possible tokens

for (String word : s.split(" "))

// if length is even

if (word.length() % 2 == 0)

// Print the word

System.out.println(word);

}

// Driver Code

public static void main(String[] args)

{

String s = "i am Geeks for Geeks and a Geek";

printWords(s);

}

}

Output:

am

Geek

Answered by CopyThat
11

Answer:

JAVA ::

Explanation:

import java.util.*;

class Brainly

{

static void main()

{

Scanner sc=new Scanner(System.in);

String s,min="",w="";

int i,c=0,f=0,l=0;

char x;

System.out.print("Enter a sentence:");

s=sc.nextLine();

s=s.trim();

s=s+" ";

for(i=0;i<s.length();i++)

{

x=s.charAt(i);

if(x!=' ')

{

w=w+x;

if(x=='a' || x=='A' || x=='e' || x=='E' || x=='i' ||

x=='I' || x=='o' || x=='O' || x=='u' || x=='U')

c++;

}

else

{

if(f==0)

{

min=w;

l=c;

f=1;

}

else

{

if(c<l)

{

l=c;

min=w;

}

}

w="";

c=0;

}

}

System.out.println("Word having minimum number of vowels:"+min);

}

}

Similar questions