Question

Write a function that will accept an array of string. Return the index of the element...

Write a function that will accept an array of string. Return the index of the element that has the most vowels. For this exercise, vowels are a,e,i,o, and u. If none of the strings contain a vowel, return -1. If two or more words contain the same largest amount of vowels, either index of such words is acceptable. (using C++)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include<iostream>

using namespace std;

//required method taking an array of strings and size of the array

int mostVowels(string array[], int size){

                //initializing variables to store the index and number of vowels for

                //the array item with max number of vowels

                int index=-1;

                int max_vowels_count=0;

                //looping through the array

                for(int i=0;i<size;i++){

                                //initializing vowels count to 0

                                int vowels_count=0;

                                //looping through each character in current string

                                for(int j=0;j<array[i].length();j++){

                                                char c=array[i][j];

                                                //checking if c is a vowel (lower case only)

                                                if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){

                                                                //updating vowels_count

                                                                vowels_count++;

                                                }

                                }

                                //if vowels_count is greater than max_vowels_count, setting i as new index

                                //and vowels_count as new max_vowels_count

                                if(vowels_count>max_vowels_count){

                                                index=i;

                                                max_vowels_count=vowels_count;

                                }

                }

                //returning index

                return index;

}

int main(){

                //creating an array of size 5

                const int size=5;

                string array[size]={"john","apples","key","fruits","avocados"};

                //finding index of array element with most vowels

                int index=mostVowels(array, size);

                //displaying result appropriately

                if(index==-1){

                                cout<<"no string has vowels in it"<<endl;

                }else{

                                cout<<"string with most vowels: "<<array[index]<<endl;

                }

                return 0;

}

Add a comment
Answer #2
#include<iostream>
using namespace std;
int highVowel(string array[], int size){

                int index=-1;

                int max_count=0;

                for(int i=0;i<size;i++){

                                int vowel_count=0;
                                for(int j=0;j<array[i].length();j++){

                                                char ch=array[i][j];

                                                if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'){

                                                                vowel_count++;

                                                }

                                }

                                if(vowel_count>max_count){

                                                index=i;

                                                max_count=vowel_count;

                                }

                }
                return index;

}

int main(){

                const int size=5;

                string array[size]={"shivi","crazy","girl","high","education"};

                int index=highVowel(array, size);

                if(index==-1){

                                cout<<"no vowels"<<endl;

                }else{

                                cout<<"string with highest no of vowels: "<<array[index]<<endl;

                }

                return 0;

}


answered by: Shivani Sharma
Add a comment
Know the answer?
Add Answer to:
Write a function that will accept an array of string. Return the index of the element...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method will accept an array of Strings. This method should return the string that...

    Write a method will accept an array of Strings. This method should return the string that comes after all the other strings in lexicographical order. (Assume all the Strings only contain lower case letters. In Example: No number, no Capitals and no Symbols) lastString({“hola”, “word”, “night”, “boom”}) → “word” lastString({“java”, “is”, “so”, “much”, “fun”}) → “so” lastString({“i”, “love”, “java”}) → “love” //precondition: words.length >0 public static String lastString(String[] words) { }

  • Write a function called most_consonants(words) that takes a list of strings called words and returns the...

    Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...

  • Write a function that returns the index of the smallest element in an array of integers....

    Write a function that returns the index of the smallest element in an array of integers. If there are more such elements than one, return the smallest index. C programming

  • Write function vowelCount2() that takes a string as input and counts and prints the number of...

    Write function vowelCount2() that takes a string as input and counts and prints the number of occurrences of vowels in the string. Vowels are a, e, i, o, and u. Implement the code in the file lab5.py. This function is very similar to the function of the problem 4.25, but the vowel information which did not appear in the string should not be printed. The printing order of the vowels is a, e, i, o, u. e.g.) >>> lab5.vowelCount2(‘augustana’) a,...

  • JAVA Array 3. Write a method called noVowels that takes a String as input and returns...

    JAVA Array 3. Write a method called noVowels that takes a String as input and returns true if it doesn't contain any vowels (a, e, i, o, u)

  • 2. Write a function convstrs that will receive a cell array of strings and a character...

    2. Write a function convstrs that will receive a cell array of strings and a character (either 'U' or 'L') as input arguments. If the character is 'U', it will return a new cell array with all of the strings in uppercase. If the character is 'L', it will return a new cell array with all of the strings in lowercase. If the character is neither 'U' nor 'L', or if the cell array does not contain all strings, the...

  • C++ 1) Write a function named WordCount, which determines the number of words in a “C...

    C++ 1) Write a function named WordCount, which determines the number of words in a “C type” string. The function will have one parameter (the address of the string) and will return the number of words (words are separated by one or more spaces). (15 points) 2) Write a function named VowelConsonant which will have three parameters - all “C type” strings. The function will place all vowels (the characters a, e, i, o, u) from the first string into...

  • Your task is to write a function definition for a function named non_vowel_words that takes a...

    Your task is to write a function definition for a function named non_vowel_words that takes a string as a parameter.  The function returns a setcontaining the words of the strings that do not start with a vowel (upper or lower case). The vowels are A, E, I, O, U. For full credit, make sure your implementation is pythonic, uses Python built-in capabilities and follows the Python style guide recommendations.  Please include a docstring for your function. You may test your function as...

  • Write a Haskell function piglatinize that returns a word into its piglatin form: if it begins...

    Write a Haskell function piglatinize that returns a word into its piglatin form: if it begins with a vowel, add to the end "yay", else move non-vowels to the end of the string until a vowel is at the front and then add to the end "ay". The word arguments are guaranteed to have a vowel (a, e, i, o, or u) and not begin with the letter y. piglatinize :: String -> String

  • Pig Latin program using Linked Lists C++

    Write a program that prompts the user to input a string and then outputs the string in the pig Latin form. The rules for converting a string into pig Latin form are asfollows:a. If the string begins with a vowel, add the string "-way" at the end of the string. for example, the pig Latin form of the string "eye" is "eye-way".b. If the string does not begin with a vowel, first ass "-" at the end of the string....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT