Question

In: Computer Science

Write a method in C# which takes string as an argument and extracts all words of...

Write a method in C# 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().

Solutions

Expert Solution


using System;
using System.Collections.Generic;

class MainClass {
  public static void Main (string[] args) {
    string sentence = "Welcome to array world shy fly crypt";
    List<string> extractedWords = extract(sentence);
    Console.WriteLine("[{0}]", string.Join(", ", extractedWords));
  }
  
  public static List<string> extract(string sentence){
    // splitting each word
    string[] words = sentence.Split(' ');
    // we dont know how many elements satisfy the condition so
    // we are using List Collection
    List<string> newList = new List<string>();
    for(int i=0;i<words.Length;i++){
      string word = words[i];
      // checking length
      bool lengthFlag = word.Length == 4 || word.Length == 5;
      // checking is vowel or not
      bool vowelFlag = word.Contains("a")||word.Contains("A")||
                        word.Contains("e")||word.Contains("E")||
                        word.Contains("i")||word.Contains("I")||
                        word.Contains("o")||word.Contains("O")||
                        word.Contains("u")||word.Contains("U");
      if( lengthFlag && vowelFlag ){
        newList.Add(words[i]);
      }
    }
    return newList;
  }
}


Code

Output


Related Solutions

Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Write a recursive method that takes a String argument and recursively prints out each word in...
Write a recursive method that takes a String argument and recursively prints out each word in the String on a different line. Note: You will need to use methods such as indexOf() and substring() within the String class to identify each word and the remaining string. For example, if the input was “the cat purred”, then the method would print the following to the Java console: the cat purred Challenge Problem Write a recursive method that takes a string as...
Write a function that takes a string as an argument checks whether it is a palindrome....
Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and L2=[] ....
IN JAVA A recursive method that takes a String as its argument and returns a list...
IN JAVA A recursive method that takes a String as its argument and returns a list of Strings which includes all anagrams of the String. This method will contain a loop that generates each possible substring consisting of all but one character in the input String, ie the substring that omits the first letter, then the substring that omits the second letter, etc. Within the loop, the method calls itself recursively for each substring. For each String in the list...
Write a method called isMatch that takes a string of parentheses and check if all parentheses...
Write a method called isMatch that takes a string of parentheses and check if all parentheses match correctly. The parameter string might consist of other characters. Ignore other characters, just check the () {} [] public static boolean isMatch(String expressionLine); Use a JCF ArrayDeque or LinkedList as a stack to store all open or left parentheses. Use following main method to check your method. public static void main(String[] args) { String[] expression = new String[]{"{5*(x+2)+(y-1);}", "32*(20+(x[i]*3)-1", "((){([][])})", "({}((0))", "{([]})", "{}())",...
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
Write a method that will have a C++ string passed to it. The string will be...
Write a method that will have a C++ string passed to it. The string will be an email address and the method will return a bool to indicate that the email address is valid. Email Validation Rules: ( These rules are not official.) 1) No whitespace allowed 2) 1 and only 1 @ symbol 3) 1 and only 1 period allowed after the @ symbol. Periods can exist before the @ sign. 4) 3 and only 3 characters after the...
Write a method that will have a C++ string passed to it. The string will be...
Write a method that will have a C++ string passed to it. The string will be an email address and the method will return a bool to indicate that the email address is valid. Email Validation Rules: ( These rules are not official.) 1) No whitespace allowed 2) 1 and only 1 @ symbol 3) 1 and only 1 period allowed after the @ symbol. Periods can exist before the @ sign. 4) 3 and only 3 characters after the...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT