Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!