2.编写一个程序,将句子拆分成单词数组,然后选择第一个字母为元音的单词. [英] 2. Write a program that Split the sentence into an array of words and select those whose first letter is a vowel.
本文介绍了2.编写一个程序,将句子拆分成单词数组,然后选择第一个字母为元音的单词.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编写一个程序,将句子拆分成单词数组,然后选择第一个字母为元音的单词.
Write a program that Split the sentence into an array of words and select those whose first letter is a vowel.
推荐答案
public System.Collections.ArrayList getVowelWord(string sentence)
{
System.Collections.ArrayList arr = new System.Collections.ArrayList();
string[] words = sentence.Split(' ');
string[] vowels = new string[]{ "A", "E", "I", "O", "U" };
foreach (string s in words)
{
foreach (string vowel in vowels)
{
if (s.ToUpper().IndexOf(vowel) == 0)
{
arr.Add(s);
break;
}
}
}
return arr;
}
using System;
class Program
{
static void Main()
{
string s = "there is a cat";
//
// Split string on spaces.
// ... This will separate all the words.
//
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}
这篇关于2.编写一个程序,将句子拆分成单词数组,然后选择第一个字母为元音的单词.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文