Pig Latin

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3159 Views - Last Post: 29 July 2014 - 03:16 PM Rate Topic: -----

#1 RoisinMD   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-March 14

Pig Latin

Posted 28 July 2014 - 06:04 PM

So I have a pig latin assignment due,
I have got help from other forums but the code was simply too advanced for me, so I have gone back to my original code with pieces from over the internet,
I am running into a problem, my code is only translating the last word entered not all words?
<code>
List<string> words = new List<string>();
         string english = Convert.ToString(txtEng.Text);
         words.Add(english);

            
            string pig = "";
            string first;
            string rest;
            string vowels = "aeiouAEIOU";
            int letter;
           
            string[] Words = txtEng.Text.Split(' ');

            foreach (string word in Words
)
            {

                {
                    first = word.Substring(0, 1);
                    rest = word.Substring(1, word.Length - 1);
                    letter = vowels.IndexOf(first);
                    if (letter == -1)
                    {
                        pig = rest + first + "ay";
                    }
                    else
                    {
                        pig = first + rest + "way";
                    }

                } txtPig.Text = pig.ToString();

            }
        }
 </code>

This post has been edited by modi123_1: 28 July 2014 - 06:06 PM
Reason for edit:: please use the code tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Pig Latin

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,364
  • Joined: 12-June 08

Re: Pig Latin

Posted 28 July 2014 - 06:07 PM

Why the extra set of { and }? Line 19 and 32?


32	                } txtPig.Text = pig.ToString();

You know the '=' sign is for "assignment" right? As in - remove what ever was there and put this there instead? Why not try append, concatenate, or a simple += ?
Was This Post Helpful? 0
  • +
  • -

#3 RoisinMD   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-March 14

Re: Pig Latin

Posted 28 July 2014 - 06:15 PM

Oh wow...I didn't even realise that I hadn't put the += there...well guess who feels like an idiot.
I put that in , both words translate but now without a space in between? Is there a problem with my split?

(Very new...in case you hadn't guessed)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,364
  • Joined: 12-June 08

Re: Pig Latin

Posted 28 July 2014 - 06:18 PM

If you want a space then add a space. Append it..
Was This Post Helpful? 0
  • +
  • -

#5 RoisinMD   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-March 14

Re: Pig Latin

Posted 28 July 2014 - 06:20 PM

Never mind got the spaces to work :)

One more question?

When it translates a consonant it should keep going until it finds the first vowel i.e this should become isthay but atm i am getting histay....do I have to create a loop to check each character entered? or?
Was This Post Helpful? 1
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,364
  • Joined: 12-June 08

Re: Pig Latin

Posted 28 July 2014 - 06:22 PM

Quote

do I have to create a loop to check each character entered?

It sounds like a pattern, right? If you want to iterate through an array of characters what's the best method? A loop.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7087
  • View blog
  • Posts: 24,090
  • Joined: 05-May 12

Re: Pig Latin

Posted 28 July 2014 - 06:25 PM

I'm really glad you got this thing figured out. Hurrah!

Yes, instead of just grabbing the first letter, you should keep grabbing letters until you hit a vowel.

Once you figure out the easy general cases, make a back up of your code, then tackle the odd cases like weird composed of all consonants, and words that start with silent letters, and compound errors with or without hyphens.
Was This Post Helpful? 0
  • +
  • -

#8 RoisinMD   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-March 14

Re: Pig Latin

Posted 28 July 2014 - 06:29 PM

^ Skydiver haha thank you, I was having a bad day yesterday sorry!!

Yeah 'easy parts' im sure in a few weeks I will look back and say how dumb was I!!

Yup I get the idea that I have to keep going until I hit a vowel...any tips on how to actually do it?
atm thats my problem I have it all figuered out how I have to do it...but when it comes to implementing the code its a different story!!
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,364
  • Joined: 12-June 08

Re: Pig Latin

Posted 28 July 2014 - 06:30 PM

A string is just like an array. How do you loop through an array?

http://msdn.microsof...y/2h3zzhdw.aspx
Was This Post Helpful? 0
  • +
  • -

#10 RoisinMD   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-March 14

Re: Pig Latin

Posted 28 July 2014 - 06:42 PM

foreach loop...yep I get that...what Im saying is I dont know how to code it that it keeps checking through until it gets to the first vowel..I am having a complete mind meltdown!
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,364
  • Joined: 12-June 08

Re: Pig Latin

Posted 28 July 2014 - 06:46 PM

Use a 'break' to stop a for loop for what ever condition you decide.

http://msdn.microsof...y/adbctzc4.aspx
Was This Post Helpful? 0
  • +
  • -

#12 RoisinMD   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-March 14

Re: Pig Latin

Posted 28 July 2014 - 06:48 PM

Hmm ok...

thanks
Was This Post Helpful? 0
  • +
  • -

#13 RoisinMD   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-March 14

Re: Pig Latin

Posted 28 July 2014 - 10:00 PM

Hmm...
So I've found an interesting error.. I put in code to say if it was a number just leave it don't add an ending and it works fine if I type just a number it works but say if I type " This 1" then it repeats twice and doesnt translate? so i get this 1 this 1??



private void btnTranslate_Click(object sender, EventArgs e)
        {
            if (txtEng.Text.Length == 0)
            {
                MessageBox.Show("You Must Enter text to Translate");
            }

             List<string> words = new List<string>();
         string english = Convert.ToString(txtEng.Text);
         words.Add(english);


         string pig = " ";
            string first;
            string rest;
            string vowels = "aeiouAEIOU";
            int letter;
  
            string[] Words = txtEng.Text.Split(' ');

            foreach (string word in Words)
            {

                
                    first = word.Substring(0, 1);
                    rest = word.Substring(1, word.Length - 1);
                    letter = vowels.IndexOf(first);

                    string number = txtEng.Text;
                    if (number.Any(Char.IsDigit))//checks if there is any numbers
                    {
                        pig = number; //if there is do not translate
                    }
                
                    else  if (letter == -1)
                    {
                        pig = " " + rest + first + "ay";
                    }
                    else 
                    {
                        pig = " " + first + rest + "way";
                    }
                    
            

                 txtPig.Text += pig.ToString();

            }
        }

Was This Post Helpful? 0
  • +
  • -

#14 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Pig Latin

Posted 29 July 2014 - 03:29 AM

What happens when you put words that start with vowels? Like It, Ultimate, egg it should say it-yay, ultimate-yay and egg-yay. Words that start with vowels just have -yay at the end of the word. Here are the rules for pig latin.

This post has been edited by Michael26: 29 July 2014 - 03:31 AM

Was This Post Helpful? 0
  • +
  • -

#15 RoisinMD   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-March 14

Re: Pig Latin

Posted 29 July 2014 - 03:31 AM

That's not the requirements I have been given. Words that starts with vowels have 'way' added to the end words that dont, put all the consonants to the end of the word until you come to a vowel and add 'ay'
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2