Program firstvowel.cs
Demonstrates usage of the String.Substring() and String.IndexOf() methods
using System;
/*
* Written by Bryan Miller on March 30, 2007
* wmmiller@duo-county.com or kyrathaba@yahoo.com
* http://kyrathaba.dcmembers.com
* http://www1.webng.com/bowrsanryld/
*/
class Program {
static void Main(string[] args) {
string vowels = "AEIOUaeiou"; //the vowels
string currentLetter = string.Empty; //the character we're currently examining in the word
string word = string.Empty; //the word entered by the user
int wordlen = -1; //will hold length of word entered by user
int index = -1; //will hold value corresponding to current position that we're evaluating within the word
Console.WriteLine("For our purposes, \"Y\" is not considered among the vowels...\n");
//unless and until user enters the word "quit", do all this...
do {
//ensure that user enters a string of characters whose length is between 3 and 15, inclusive
do {
Console.WriteLine("Please enter a word that is 3 to 15 characters long, ");
Console.Write("or enter \"quit\" to exit program: ");
word = Console.ReadLine().Trim();
wordlen = word.Length;
Console.WriteLine();
if (wordlen < 3) { Console.WriteLine("Your word should be at least 3 characters in length.\n"); } //end if
if (wordlen > 15) { Console.WriteLine("Your word should be no more than 15 characters in length.\n"); } //end if
} while ((wordlen < 3) || (wordlen > 15)); //end do-while
/*
* The following for loop steps through the individual letters of the word entered by the user, beginning with the
* first letter of that word and, if necessary, going all the way to the last letter. For each letter in the word,
* we use the .IndexOf String instance method to determine if that letter is a vowel or not. If it IS, we set our
* integer variable named index equal to i, and since i is the index of the current letter we're examining, index
* will -- as we break out of the for loop -- hold the index at which the first vowel occurs in the word. We could
* have completed the for loop, stepping through each and every element of the word. But if the first vowel doesn't
* occur at the very last position in the word, it would be inefficient. Better to break out of the for loop as soon
* as we have located the very first vowel.
*/
for (int i = 0; i < wordlen; i++) {
currentLetter = word.Substring(i, 1);
index = vowels.IndexOf(currentLetter);
if (index != -1) {
index = i;
break;
}//end if
}//end for loop
/* if the word the user entered is "quit", then user wants to exit program and isn't interested in the information
* contained in the following Console.WriteLine() invocations */
if (word != "quit") {
if (index != -1) {
Console.WriteLine("The first vowel, \"{0}\", occurred at position {1} in \"{2}\"", currentLetter, index, word);
Console.WriteLine("It is the {0}", PositionOfLetter(index,wordlen));
} else {
Console.WriteLine("There were no vowels found, therefore the string you entered");
Console.WriteLine("cannot properly be called a word.");
}//end if
}//end if
Console.WriteLine();
} while (word.ToLower() != "quit"); //end outer do-while
}//end Main
private static string PositionOfLetter(int index, int wordlen) {
/*
* The 0th position is the word is the 1st character. The 4th position is the 5th character, etc.
* This switch statement maps this relationship, and allows us to build up the returnVal string as
* a return value, making this private static method useful to the program's output
*/
string returnVal = string.Empty;
switch (index) {
case 0:
returnVal = "1st";
break;
case 1:
returnVal ="2nd";
break;
case 2:
returnVal ="3rd";
break;
case 3:
returnVal ="4th";
break;
case 4:
returnVal = "5th";
break;
case 5:
returnVal = "6th";
break;
case 6:
returnVal ="7th";
break;
case 7:
returnVal ="8th";
break;
case 8:
returnVal ="9th";
break;
case 9:
returnVal ="10th";
break;
case 10:
returnVal ="11th";
break;
case 11:
returnVal ="12th";
break;
case 12:
returnVal = "13th";
break;
case 13:
returnVal ="14th";
break;
case 14:
returnVal ="last";
break;
default:
returnVal ="unknown error in switch statement";
break;
}//end switch
returnVal += " of " + wordlen.ToString() + " characters in the word";
return returnVal;
}//end private static string method
}//end class Program