PigLatin program, calling methods.

Could someone please help me to understand this small program below?

[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PigLatin
{
class Pig
{
static void Main(string[] args)
{
string pigWord = "";
string sentence = "";
string firstLetter;
string restOfWord;
string vowels = "AEIOUaeiou";
int letterPos;

while(sentence.ToLower() != "quit")
{
Console.WriteLine("Please enter a sentence or type "quit" to exit");
sentence = Console.ReadLine();

foreach (string word in sentence.Split())
{
firstLetter = word.Substring(0, 1);
restOfWord = word.Substring(1, word.Length - 1);

letterPos = vowels.IndexOf(firstLetter);

if (letterPos == -1)
{
// it's a consonant
pigWord = restOfWord + firstLetter + "ay";
}
else
{
// it's a vowel
pigWord = word + "way";
} // end if

Console.Write("{0} ", pigWord);
} // end foreach
} // end while
} // end Main
} // end class
} // namespace[/code]

This is the part I'M having the most trouble with.
[code]
foreach (string word in sentence.Split())
{
firstLetter = word.Substring(0, 1);
restOfWord = word.Substring(1, word.Length - 1);

letterPos = vowels.IndexOf(firstLetter);

if (letterPos == -1)[/code]

Comments

  • [code]
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace PigLatin
    {
    class Pig
    {
    static void Main(string[] args)
    {
    string pigWord = "";
    string sentence = "";
    string firstLetter;
    string restOfWord;

    // 'vowels' is just being used as a 'list' of vowels.
    // The code will take the first letter of a word and see if
    // it is in this list - if so, then it knows that the
    // letter is indeed a vowel - but that comes later...
    string vowels = "AEIOUaeiou";

    int letterPos;

    // This is a loop that will (when this is run) keep asking
    // for another sentence until you type the word 'quit'
    while(sentence.ToLower() != "quit")
    {
    // This prints the user instructions to the screen
    Console.WriteLine("Please enter a sentence or type "quit" to exit");
    // This tells the computer to wait for the user to
    // type a new sentence.
    sentence = Console.ReadLine();
    [/code]

    Ok - so the next part is accessing a method that is defined in the 'string' class that will 'split' the string up into an array of smaller strings.
    The variable 'sentence' is a string that contains a bunch of characters (letters and spaces).
    The .Split() method takes whatever the value of the string you are 'splitting' and breaks it down into smaller chunks. In this case, Split is breaking up sentence every place that there is a space - so if sentence were to hold the value "This is my sentence" - Split would return 4 smaller strings in an array "This", "is", "my", and "sentence".
    You can actually specify something other than a space to seperate the string by - but space is the default.

    Any time you see 'foreach' the program is being told to cycle through a collection of items. In this case you are cycling through each smaller string that was pulled out of 'sentence'. In the loop shown here a variable named 'word' is being assigned the value of on of the smaller strings and will contain a different value each time the loop is itterated through. This way the same code is applied to each of the smaller strings without having to write new code for each of the smaller strings.
    [code]
    foreach (string word in sentence.Split())
    {
    [/code]

    Now, just like the .Split() method - string also has another method called 'Substring' attached to it. What Substring will do is take a string and extract a portion of it starting at the specified 'index' and of the specified 'length'.
    In the case of 'firstLetter' below, this is returning a section from the variable 'word' that starts at the first character (index 0) and is 1 letter long. So if the variable 'word' contains the value "This" (from my previous example) then the variable 'firstLetter' will now hold the letter "T". If you were to instead write "word.Substring(0,2)" you would get the first 2 letters instead of the first 1 letter "Th".

    The following statement is simply grabbing everything after the first letter and could actually just be written like so:
    restOfWord = word.Substring(1);

    Any time you just want to get a portion of a string from the index to the end of the string, you can simply ommit the length parameter.
    [code]
    firstLetter = word.Substring(0, 1);
    restOfWord = word.Substring(1, word.Length - 1);
    [/code]

    Now, it looks like this code was either written a while ago or by a developer who may not be entirely familiar with the .NET library- Because the next statement could actually be written a way that is a tiny bit easier to understand.
    The person here used yet another string method "IndexOf". This method will look for one string inside of another string. So if the variable 'firstLetter' contained "A", this method would look in the string variable named 'vowels' for the letter A and would find that it is the first letter. So in that case it would return a 0 - meaning that it found the letter "A" at index 0 in the string "AEIOUaeiou". (remember from at the top of the code?)

    But if you take my previous example and the variable 'firstLetter' was holding the value "T" - the method IndexOf would return a -1 because it did not find the letter "T" at all.

    The author of this code is simply trying to determine if the first letter of each word is a vowel or not. This can also be done by using a different method on the string class called 'Contains'.
    Contains will return either true or false instead of an index - so ultimately you will have the same result - but one makes a little more sense than the other.
    [code]
    letterPos = vowels.IndexOf(firstLetter);

    if (letterPos == -1)
    {
    // it's a consonant
    // so IF the first letter was not a vowel
    // then put the first letter of the word
    // at the end and add "ay" to the result
    pigWord = restOfWord + firstLetter + "ay";
    }
    else
    {
    // it's a vowel
    // OTHERWISE just add "way" on to the original
    // word.
    pigWord = word + "way";
    } // end if

    // Then write the resulting pig-latin word to the
    // screen and put a space after it. This will
    // eventually create a pig latin sentence.
    Console.Write("{0} ", pigWord);

    // This is the end of the 'foreach' loop which will
    // keep repeating until it runs out of words from the
    // sentence entered by the user.
    } // end foreach

    // This is the end of the 'while' loop that will keep
    // repeating until the user types quit
    } // end while

    // Basically this is the end of your program.
    } // end Main

    // This is the end of the class that contains your program
    } // end class
    // Worry about namespaces later.
    } // namespace
    [/code]

    did that clear it up any?

    ><//~Psightoplasm`~
  • Yes thanks, I understand it better now.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories