english to pig latin translator

c# .net translator not giving pig latin output

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 28104 Views - Last Post: 07 June 2016 - 04:15 AM Rate Topic: -----

#1 tricket_7   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

english to pig latin translator

Posted 21 June 2010 - 05:40 PM

This program is driving me nuts. Instead of giving me the pig latin output this is what I get in the pig latin text box
System.Windows.Forms.TextBox, Text: System.Windows.Forms.TextBox, Text: Syst...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project_9_1
{
    public partial class frmPigLatinTranslator : Form
    {
        public frmPigLatinTranslator()
        {
            InitializeComponent();
        }

        private void btnTranslate_Click(object sender, EventArgs e)
        {
           
           string english = Convert.ToString(txtEnglish.Text);
           string pigLatin = "";
           string firstLetter;
           string restOfWord;
           string vowels = "AEIOUaeiou";
           int letterPos;
                  

          foreach (string word in english.Split())
          {
              firstLetter = word.Substring(0, 1);
              restOfWord = word.Substring(1, word.Length - 1);
              letterPos = vowels.IndexOf(firstLetter);
              if (letterPos == -1)
              {
                  //it's a consonant
                  pigLatin = restOfWord + firstLetter + "ay";
              }
              else
              {
                  //it's a vowel
                  pigLatin = word + "way";
              }
                  txtPigLatin.Text = txtPigLatin.ToString();

                  txtEnglish.Focus();
              }
          }
      

           
           


          
        }

       
    }


Am I missing something? Please help

Is This A Good Question/Topic? 0
  • +

Replies To: english to pig latin translator

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: english to pig latin translator

Posted 21 June 2010 - 05:59 PM

txtPigLatin.Text = txtPigLatin.ToString(); 


You are telling your textbox to hold in the Text property the value of the method ToString() on your textbox. You probably want:

txtPigLatin.Text = pigLatin;

Was This Post Helpful? 0
  • +
  • -

#3 tricket_7   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: english to pig latin translator

Posted 21 June 2010 - 06:01 PM

I got that working, but now it is only translating the last word in the sentence.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: english to pig latin translator

Posted 21 June 2010 - 06:11 PM

You need to perform the conversion on each word in the sentence. You are treating the sentence as one word now. What I would do is move the 'english to piglatin' code into it's own method, split the sentence into words (Look at String.Split()) and use StringBuilder to build up my converted sentence.
Was This Post Helpful? 0
  • +
  • -

#5 tricket_7   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: english to pig latin translator

Posted 21 June 2010 - 06:14 PM

? I have struggled so much with this exercise, does that mean concatate?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: english to pig latin translator

Posted 21 June 2010 - 06:36 PM

This line
txtPigLatin.Text = txtPigLatin.ToString();
You are setting the text of the text box to the *type* of the the text box itself, which is Of course
System.Windows.Forms.Textbox

It is giving you exactly what you asked for. Problem is, you don't k ow what it is you need to ask for
Was This Post Helpful? 0
  • +
  • -

#7 tricket_7   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: english to pig latin translator

Posted 21 June 2010 - 07:01 PM

I got that to work, I realized what I did wrong there, but still trying to figure out how to get the whole sentence to translate, not just the last word
Was This Post Helpful? 0
  • +
  • -

#8 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: english to pig latin translator

Posted 21 June 2010 - 08:18 PM

List<string> Words = textEnglish.Text.Split(' ');

foreach(string word in Words)
{
    ApplyConversion(word);
}

//Now this is the ApplyConversion method.
public void ApplyConversion(word)
{
    //Your logic for changing a single word would go here.
    //Maybe you could concatenate the result to the existing .Text in 
    //the textbox.
    textPigLatin.Text += yourTranslatedWord;
}

This post has been edited by stapia.gutierrez: 22 June 2010 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: english to pig latin translator

Posted 22 June 2010 - 01:44 AM

List<string> Words = textEnglish.Text;

That line isn't going to work. TextBox.Text is a string, not a List<string>. To generate a List, you'll need to .Split the string into the List first.
Was This Post Helpful? 0
  • +
  • -

#10 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: english to pig latin translator

Posted 22 June 2010 - 08:59 AM

Yeah, forgot about that last night.
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: english to pig latin translator

Posted 22 June 2010 - 09:20 AM

View Poststapia.gutierrez, on 21 June 2010 - 10:18 PM, said:

List<string> Words = textEnglish.Text.Split(' ');


That doesn't work either since Split returns an array.

You could do this...

List<string> Words = new List<string>(textEnglish.Text.Split(' '));

// or simply use the array...
string[] Words = textEnglish.Text.Split(' ');


This post has been edited by eclipsed4utoo: 22 June 2010 - 09:21 AM

Was This Post Helpful? 1
  • +
  • -

#12 bethsedea   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-July 12

Re: english to pig latin translator

Posted 19 July 2012 - 01:51 AM

the answer to this problem:

if (letterPOS == -1)
{
pigLatin += others + firstL + "ay ";
}
else
{
pigLatin += word + "way ";
}
textBox2.Text = pigLatin;

do add += in your code...
you just forgot to append the words. :)
Was This Post Helpful? 0
  • +
  • -

#13 chrisychurch   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-August 13

Re: english to pig latin translator

Posted 20 August 2013 - 07:30 AM

If you are interested, here is some code I did for a English to pig Latin converter on the C# form:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace question8chap3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String NormalWord;
            String PiggedWord;

            String Firstletter;
            String Restofword;

            NormalWord = textBox1.Text;
            Firstletter = NormalWord.Substring(0, 1);
            Restofword = NormalWord.Substring(1);

            PiggedWord = Restofword + Firstletter + "ay";

            label3.Text = PiggedWord;

        }
    }
}




Hope this can be of assistance!
Was This Post Helpful? 0
  • +
  • -

#14 reigh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-March 16

Re: english to pig latin translator

Posted 04 June 2016 - 11:59 PM

I just did this for a class of mine. I however took a shortcut when dealing with Y. I basically just set up logical decision maker to decide if a word contained no vowels like for instance in the word "why" so that it would not throw an exception.

True pig latin treats Y as a vowel in some cases and not others. For instance if a word starts with Y then Y is treated as a consonant and is moved to the end like in the word "Yellow" being ellowyay. If it is the second letter of a two letter word it is treated like a vowel and not moved like in the word "my" being ymay. I the case of y being after several consonants it is treated as a vowel like in the word "rhythm" being ythmrhay. See the definitions here http://www.wikihow.com/Speak-Pig-Latin

In the following code those three would be Yellow = ellowyay, my = myay, and rhythm = rhythmay because my code simply changes the word at the point in the substring it finds a vowel. If someone want to write some code to determine how to better split up these cases I'm all ears. I might guess that most cases could be covered within the else statement were I know I have no vowels. Within that else a letter search for the position of Y and split the word there into substrings. I have no definitive evidence that that is always the case even though that would work for the three examples I gave. Although I do not know if that will always work in my second body of code I search the words without aeiou in them for y and split the word there as if y was a vowel.

Example 1
//Jordan Walker
//06/04/2016

using System;
using static System.Console;

namespace _07LanguageTranslator
{
    class PigLatin
    {
        static void Main()
        {
            //Declar Vars
            string input = "", repeat = "Y", result = "";

            //Output greeting
            DisplayGreeting();

            //Loop construct 
            do
            {

                //Prompt user for a sentence
                Write("\nEnter a sentence with no punctuation: ");
                input = ReadLine();

                result = Translate(input);
                //Display Output
                WriteLine("Here is the sentence in Pig Latin: " + result);

                //Promt to repeat
                Write("\nDo you want to translate another Sentence?" +
                      "\nType y to continue or any other key to end the program: ");
                repeat = ReadLine();

            } while (repeat.ToLower() == "y");

            //pause
            ReadKey();
        }

        //method to display a greeting
        private static void DisplayGreeting()
        {
            WriteLine("\nThis program will prompt for" +
                      "\na sentence, and then translate it into" +
                      "\n***** Pig Latin *****");
        }

        //method to translate sentences into pig latin
        private static string Translate(string sentence)
        {
            //declare vars
            string modString = "";
            string[] allWords = sentence.ToLower().Split(' ');

            //process to pig latin for each word
            foreach (string word in allWords)
            {
                modString += TranslateWord(word);
                modString += " ";
            } 

            WriteLine("\nThis is my translator stub.");

            return modString.TrimEnd();
        }

        private static string TranslateWord(string word)
        {
            //declare vars
            const string YAY = "yay";
            const string AY = "ay";
            const string VOWEL = "aeiou";
            string modString = ""; //returned var
            int position = 0;
            int count = 0;
            char letter = word[0];
            bool hasVowels = true;

            //process data
            position = VOWEL.IndexOf(letter);

            while (position == -1 && count <= word.Length)
            {
                if (position == -1 && count < word.Length)
                {
                    ++count;
                    letter = word[count-1];
                    position = VOWEL.IndexOf(letter);
                    hasVowels = true;
                    
                }
                else
                {
                    ++count;
                    hasVowels = false;
                }
            }

            letter = word[0];
            position = VOWEL.IndexOf(letter);
            if (position != -1) //means it must be a vowel
            {
                modString = word + YAY;
            }
            else if (hasVowels)
            {
                count = 0;
                while (position == -1 && count < word.Length)
                {
                    ++count;
                    letter = word[count];
                    position = VOWEL.IndexOf(letter);
                }
                //create new string
                modString = word.Substring(count) + word.Substring(0, count) + AY;
            }
            else
            {
                count = 0;
                //create new string
                modString = word.Substring(count) + word.Substring(0, count) + AY;
            }

            return modString;
        }
    }
}




example 2
//Jordan Walker
//06/04/2016

using System;
using static System.Console;

namespace _07LanguageTranslator
{
    class PigLatin
    {
        static void Main()
        {
            //Declar Vars
            string input = "", repeat = "Y", result = "";

            //Output greeting
            DisplayGreeting();

            //Loop construct 
            do
            {

                //Prompt user for a sentence
                Write("\nEnter a sentence with no punctuation: ");
                input = ReadLine();

                result = Translate(input);
                //Display Output
                WriteLine("Here is the sentence in Pig Latin: " + result);

                //Promt to repeat
                Write("\nDo you want to translate another Sentence?" +
                      "\nType y to continue or any other key to end the program: ");
                repeat = ReadLine();

            } while (repeat.ToLower() == "y");

            //pause
            ReadKey();
        }

        //method to display a greeting
        private static void DisplayGreeting()
        {
            WriteLine("\nThis program will prompt for" +
                      "\na sentence, and then translate it into" +
                      "\n***** Pig Latin *****");
        }

        //method to translate sentences into pig latin
        private static string Translate(string sentence)
        {
            //declare vars
            string modString = "";
            string[] allWords = sentence.ToLower().Split(' ');

            //process to pig latin for each word
            foreach (string word in allWords)
            {
                modString += TranslateWord(word);
                modString += " ";
            }

            WriteLine("\nThis is my translator stub.");

            return modString.TrimEnd();
        }

        private static string TranslateWord(string word)
        {
            //declare vars
            const string YAY = "yay";
            const string AY = "ay";
            const string VOWEL = "aeiou";
            const string Y = "y";
            string modString = ""; //returned var
            int position = 0;
            int count = 0;
            char letter = word[0];
            bool hasVowels = true;

            //process data
            position = VOWEL.IndexOf(letter);

            while (position == -1 && count <= word.Length)
            {
                if (position == -1 && count < word.Length)
                {
                    ++count;
                    letter = word[count - 1];
                    position = VOWEL.IndexOf(letter);
                    hasVowels = true;

                }
                else
                {
                    ++count;
                    hasVowels = false;
                }
            }

            letter = word[0];
            position = VOWEL.IndexOf(letter);
            if (position != -1) //means it must be a vowel
            {
                modString = word + YAY;
            }
            else if (hasVowels)
            {
                count = 0;
                while (position == -1 && count < word.Length)
                {
                    ++count;
                    letter = word[count];
                    position = VOWEL.IndexOf(letter);
                }
                //create new string
                modString = word.Substring(count) + word.Substring(0, count) + AY;
            }
            else
            {
                count = 0;
                while (position == -1 && count < word.Length)
                {
                    ++count;
                    letter = word[count];
                    position = Y.IndexOf(letter);
                }
                //create new string
                modString = word.Substring(count) + word.Substring(0, count) + AY;
            }

            return modString;
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#15 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: english to pig latin translator

Posted 05 June 2016 - 01:49 AM

Please be aware that this question was originally posted in 2010.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2