Monday, August 27, 2012

.NET (C#) based PigLatin generator

Hi All,

I was watching a tv show this morning about piglatin, so I thought I will write this during my coffee break. Enjoy. This is not a complete implementation, well don't expect too much for a coffee break project. :)


  public static string GetPigLatinFromText(string strText)
        {
            string strRetVal = string.Empty;
            int i,j;
            try
            {
                 
                StringBuilder sb = new StringBuilder();
                char[] chrLetters ;
                string[] strWord = strText.Split(' ', '\n');

                for ( i= 0; i < strWord.Length; i++)
                {
                    chrLetters = strWord[i].ToCharArray();
                    if (chrLetters.Length > 2)
                    {
                        if (chrLetters[0] == 'a' || chrLetters[0] == 'e' || 
                            chrLetters[0] == 'i' || chrLetters[0] == 'o' || 
                            chrLetters[0] == 'u' ||
                            chrLetters[0] == 'A' || chrLetters[0] == 'E' || 
                            chrLetters[0] == 'I' || chrLetters[0] == 'O' || 
                            chrLetters[0] == 'U')
                        {
                            //Starts with a vowel
                            sb.Append(strWord[i]);
                            sb.Append("ay ");


                        }
                        else if (chrLetters[0] == 'g' || chrLetters[0] == 'G')
                        {

                            for (j = 1; j < chrLetters.Length; j++)
                            {
                                sb.Append(Char.ToLower(chrLetters[j]));
                            }

                            sb.Append('g');
                            sb.Append("way ");
                        }
                        else
                        {
                            for (j = 1; j < chrLetters.Length; j++)
                            {
                                sb.Append(Char.ToLower(chrLetters[j]));
                            }

                            sb.Append(Char.ToLower(chrLetters[0]));
                            sb.Append("ay ");

                        }

                    }
                    else
                    {
                        sb.Append(strWord[i]);
                        sb.Append(" ");
                    }
                }

                strRetVal = sb.ToString();
            }
            catch (Exception exp)
            {
                throw exp;
            }

            return strRetVal;
        }

No comments: