Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java prohram that accepts a non-empty array, inStrArr(String[]) as input parameter and returns a non-empty array,outStrArr (String[]) based on the below logic: -

Write a Java prohram that accepts a non-empty array, inStrArr(String[]) as input parameter and returns a non-empty array,outStrArr (String[]) based on the below logic:

- Consider each element in the inStrArr

- If the element consist of alternating vowels and consonants,add the element to outStrArr

Note: Perform case-insensitive operation

- If the outStrArr is empty, i.e. the outStrArr has no elements,add "X" to outStrArr

-Return the outStrArr

Assumptions:

-Each element of inStrArr would contain only alphabets

-The elements of inStrArr would be unique

-Alternate vowels and consonants can imply 'a vowel followed bya consonant' or 'a consonants followed by a vowel".

-Each String element of the inStrArr will have more than 1character

Note: No need to validate the assumptions

Example:

inStrArr: {"bAcon", "nation", "dog", "dEcldEd", "cuisine"}

outStrArr: {"bAcon" "dog", "dEcldEd"}

Explanation:

-In the above example, the first element of inStrArr is "bAcon".Here, the element starts with 'b' (consonant) followed by 'A'(vowel) followed by 'c' (consonant) followed by 'o' (vowel)followed by 'n' (consonant). Since the element consists ofalternating vowels and consonants, add it to the outStrArr.

-The next element in inStrArr is "nation". Here, two vowels 'i'and 'o' occur subsequently. Hence, do not add it to outStrArr.

-The next element in inStrArr is "dog". Here the vowel 'o'occurs between the consonants 'd' and 'g'. Since the elementconsists of alternating vowels and consonants, add it to theoutStrArr.

I've attached my code below. I don't know where I did wrong, Ican't get the output. Please let me know if anyone able to help meout. Thank you!

public class Solution {

public String[]alternatingVowelsConsonants(String[] inStrArr) {

String[] outStrArr =new String[1];

//Implement your logic here

String str = "";

String str1 = "";

for(int i = = 0;i

for(int j = 0; j

// if (j == 0 || j ==inStrArr[i].length() - 1){

// str +=inStrArr[i].charAt(j);

// continue;

// }

if(isVowel(inStrArr[i].charAt(j))== false &&

isVowel(inStrArr[i].charAt(j + 1))== true &&

isVowel(inStrArr[i].charAt(j + 2))== false)

{

str +=inStrArr[i].charAt(j) + " ";

continue;

} else {

str1 +=inStrArr[i].charAt(j) + " ";

}

}

}

outStrArr[0] = str;

return outStrArr;

}

static boolean isVowel(char x) {

if (x == 'a' || x == 'e' ||

x == 'i' || x == 'o' ||

x == 'u' || x == 'A' ||

x == 'E' || x == 'I' ||

x == 'O' || x == 'U') {

return true;

}else {

return false;

}

}

}

public class Tester {

public static void main(String[] args) {

String[] inStrArr = {"bAcon", "nation", "dog", "dEcldEd","cuisine"};

Solution obj = new Solution();

String[] outStrArr =obj.alternatingVowelsConsonants(inStrArr);

for (int index=0; index

if (outStrArr[index] != null) {

System.out.println(outStrArr[index]);

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

3. Describe several research techniques used in posttesting.

Answered: 1 week ago

Question

2. Measure the implicit interest rate on credit sales.

Answered: 1 week ago