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 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
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...
/** * 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 method called countChar which accepts a string parameter and a character parameter and returns...
Write a method called countChar which accepts a string parameter and a character parameter and returns an integer, that is: int countChar (String s, char c) This method should count the number of occurrences of the character c within the string s, and return the count to the caller. Also write a main method that tests countChar. All of the print statements and user interaction belong in the main method, not in countChar.
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
Write a program that reads an integer, a list of words, and a character. The integer...
Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle z then...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT