I’m learning to use the main types in C#. For this purpose I made a simple algorithm for finding vowels in a string. Here it is:
public static string Vowels()
{
string myString = "Count the number of the vowels in this string";
string output = string.Empty;
for (int i = 0; i < myString.Length; i++)
{
if (myString[i] == 'a' || myString[i] == 'e' ||
myString[i] == 'i' || myString[i] == 'o' ||
myString[i] == 'u')
{
output += myString[i].ToString().ToUpper(); //Vowel
}
}
return output;
}
The thing is that first – I don’t like so many conditions in my if statement so I think that using some referent type would be a more elegant way to hold the vowels in, and second, even if there is a good reason to choose if statement instead struct, enum or any other type I want to learn to use them too. Could you help me with the best way to look up for each character in myString string using struct or enum (which you think is better) to hold the vowels in it?
Thanks
Leron
Maybe this is what you’re looking for:
Use like this:
And:
For finding fields in a
struct(orclass, for that matter), use something like:As others have pointed out – I’m not saying this is the correct way to execute this task. I’m just answering what seems to be the main point – how to get a list of parts of these objects.