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#
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 :)
Get Answers For Free
Most questions answered within 1 hours.