Question

Write a method which takes string as an argument and extracts all words of length between...

Write a method which takes string as an argument and extracts all words of length between 4 to 5 and contains vowels in it. Method returns an array of type string containing words which satisfied above criteria. Show these words in main(). using c#  

Homework Answers

Answer #1

First we split the string into words, and then store those words in a List. Iterate over every word, and check for vowels, and for length, if both the conditions are satisfied, then add it into the resultant array.

CODE SNAPSHOTS:

OUTPUT SNAPSHOTS:

CODE:

using System;
using System.Collections.Generic;

class Sample {
    public static List<string> calc(string str){
    string[] word = str.Split(' ');
    List<string> a = new List<string>();
    for(int it=0;it<word.Length;it++){
      // see if vowels present
    bool isVowel = word[it].Contains("a")||word[it].Contains("e")||
                        word[it].Contains("i")||word[it].Contains("o")||
                        word[it].Contains("u")||word[it].Contains("A")||
                        word[it].Contains("E")||word[it].Contains("I")||
                        word[it].Contains("O")||word[it].Contains("U");
      // length should be 4 or 5
      bool isLength = word[it].Length == 4 || word[it].Length == 5;
    // if both satisfied, then add  
      if( isVowel && isLength  ){
        a.Add(word[it]);
      }
    }
    return a;
  }
  public static void Main (string[] args) {
    string str = "hello this is the string we are using, for vowels test gfhtrykj ";
    List<string> res = calc(str);
    Console.WriteLine("{0}", string.Join(", ", res));
  }
  
}

Please comment in case of doubts or queries.
Kindly upvote if you found it useful :)

Know the answer?
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
Similar Questions
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
C programming Write a function that takes in a 2D char array (string array) and return...
C programming Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together Hint: strlen(-char*-) //returns the length of a string strcat(-char* accum-, something to add) //works like string+= in java
Write a recursive method that takes as input a string s and returns a list containing...
Write a recursive method that takes as input a string s and returns a list containing all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For example, the word ‘binary’ is an anagram of ‘brainy’. Note that the output list should not contain duplicates. Java
Write a Java method that takes a String as Input and replaces the first two occurrencesof...
Write a Java method that takes a String as Input and replaces the first two occurrencesof a given character to another provided character.   For example, if the input String is “Java is Great” and asked to replace the letter a with x then the output would be: “Jxvx is Great” The method takes a String and two characters and returns the modified String. Please make your own assumptions if needed, you do not need to write the main.
1. Please write the following in C++ also please show all output code and comment on...
1. Please write the following in C++ also please show all output code and comment on code. 2. Also, use CPPUnitLite to write all test and show outputs for each test. Write CppUnitLite tests to verify correct behavior for all the exercises. The modifications are aimed at making the exercises more conducive to unit tests. Write a function that swaps (exchanges the values of two integers). Use int* as the argument type. Write a second swap function using a reference...
Write function words() that takes one input argument—a file name—and returns the list of actual words...
Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
/** * 1. Write the method plusTwo(). * * Your method will take 2 int arrays...
/** * 1. Write the method plusTwo(). * * Your method will take 2 int arrays as parameters. * Each will have a length of 2. Your method must * create and return a new array with a length of 4, * containing all their elements. * * Here are some examples: * plusTwo({1, 2}, {3, 4}) returns {1, 2, 3, 4} * plusTwo({4, 4}, {2, 2}) returns {4, 4, 2, 2} * plusTwo({9, 2}, {3, 4}) returns {9, 2,...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from a text file containing a series of course grades (a value between 0.0 and 100.0) with one grade entry per line. However, the first line in the file is an integer value specifying how many grade entries are contained in the file. 3. The Grades class contains four static methods: a. A method called loadGrades() that opens the file, reads in the data and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT