C++ Translate English Sentence to Pig Latin

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahoway
    New Member
    • Nov 2006
    • 13

    C++ Translate English Sentence to Pig Latin

    I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so that each word is translated to Pig latin seperately. Attached is my code: Thank you for your time.

    the out put for hello world comes out :ello worldhay
    I want it to say: ellohay orldway

    // testpig.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;



    bool isVowel(char ch);
    string rotate(string pStr);
    string pigLatinString( string pStr);

    int main ()
    {

    const int LENGTH = 100;
    char str [LENGTH];



    string word, result;

    cout << "Enter a string or, blank to exit." <<endl;
    cout << "The program will output the words in Pig Latin."<<endl;
    cin.getline(str ,LENGTH);
    cout<<endl;

    cout << "The pig Latin form of " << str <<" is:\n"<< pigLatinString( str) << endl;

    return 0;
    /* Process the input line word by word */



    };









    bool isVowel(char ch)
    {
    switch (ch)
    {
    case 'A': case 'E':
    case 'I': case 'O':
    case 'U':
    case 'a': case 'e':
    case 'i': case 'o':
    case 'u':
    return true;
    default: return false;
    };
    }//end isVowel

    string rotate(string pStr)

    {
    string::size_ty pe len=pStr.length ();

    string rStr;

    rStr=pStr.subst r(1,len-1)+pStr[0];////

    return rStr;
    }

    string pigLatinString( string pStr)

    {
    string::size_ty pe len;

    bool foundVowel;

    string::size_ty pe counter;

    if (isVowel(pStr[0]))

    pStr=pStr+"way" ;

    else
    {//didn't start with a vowel

    pStr = pStr + "";
    pStr=rotate(pSt r);
    len=pStr.length ();
    foundVowel=fals e;

    for (counter = 1; counter<len-1; counter++)

    if (isVowel(pStr[0]))

    {
    foundVowel=true ;
    break;
    }

    else

    pStr=rotate(pSt r);

    if (!foundVowel)

    pStr=pStr.subst r(1,len)+"way";

    else

    pStr = pStr + "ay";
    }
    return pStr;

    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by ahoway
    I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so that each word is translated to Pig latin seperately. Attached is my code: Thank you for your time.
    Check out the C++ function strtok(). I think you can use that to separate out the words, as long as you know the delimiters that will be separating them (like spaces).

    Comment

    • manontheedge
      New Member
      • Oct 2006
      • 175

      #3
      you can read it in as characters, and signal the end of a word every time it sees a space...that's one way to do it...by avoid strings entirely.

      Comment

      • ahoway
        New Member
        • Nov 2006
        • 13

        #4
        do either of you know where I get a tutorial on this? My book is not very clear and none of my professors example programs show this. Thanks

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by ahoway
          do either of you know where I get a tutorial on this? My book is not very clear and none of my professors example programs show this. Thanks
          Strtok:


          For manontheedge's method, it's less of something there would be a tutorial on, you would create a loop that accepted a char, and test it against the value for a 'space', and then did whatever else you wanted.

          Comment

          • susheelam
            New Member
            • Nov 2006
            • 3

            #6
            Originally posted by sicarie
            Strtok:


            For manontheedge's method, it's less of something there would be a tutorial on, you would create a loop that accepted a char, and test it against the value for a 'space', and then did whatever else you wanted.
            Read the whole string the way u have done in ur program.
            then extract out each word till u find a space
            copy the word to another string from subscript 1 , and concatenate 0th subscript and "way"
            continue dor the whole length of the string

            Comment

            • ahoway
              New Member
              • Nov 2006
              • 13

              #7
              I have solved the first problem, now I need help with...

              1.Program repeats until user enters a null string
              2.Prompts the user to enter a string or just return to terminate

              here is my new code: Thank you in advance!
              #include "stdafx.h"
              #include <iostream>
              #include <string>
              #include <cstdlib>
              using namespace std;



              bool isVowel(char ch);
              string rotate(string pStr);
              string pigLatinString( string pStr);

              int main ()
              {

              string word,line;

              cout << "Enter a string or, blank to exit." <<endl;
              cout << "The program will output the words in Pig Latin."<<endl;
              getline(cin, line);


              cout << "The pig Latin form of " << line <<" is:\n";




              /* Process the input line word by word */


              while ( line.length() != 0)
              {
              //cout << "==> Enter line of text or enter return to stop input\n";




              int lineLen = line.length();
              nt wordLen = line.find(' ');
              if (wordLen > 0)
              {
              word = line.substr(0,w ordLen);
              line = line.substr(wor dLen+1);

              cout << pigLatinString( word) << " ";
              }
              else
              {
              cout << pigLatinString( line) <<"\n ";

              line = "";
              }
              }


              return 0;
              };



              ////////////////////////////////////////////////////////these are words starting with a vowel
              bool isVowel(char ch)
              {
              switch (ch)
              {
              case 'A': case 'E': case 'I': case 'O':case 'U':
              case 'a': case 'e':case 'i': case 'o': case 'u':
              return true;
              default: return false;
              };
              }///////////////////////////////////////////////////////////////end isVowel

              string rotate(string pStr)

              {
              string::size_ty pe len=pStr.length ();

              string rStr;

              rStr=pStr.subst r(1,len-1)+pStr[0];

              return rStr;
              }

              string pigLatinString( string pStr)

              {
              string::size_ty pe len;

              bool foundVowel;

              string::size_ty pe counter;

              if (isVowel(pStr[0]))

              pStr=pStr+"way" ;

              else
              {//These are words that didn't start with a vowel



              len=pStr.length ();
              foundVowel=fals e;



              if (isVowel(pStr[0]))

              {
              foundVowel=true ;
              }

              else

              pStr=rotate(pSt r);

              if (!foundVowel)

              pStr=pStr.subst r(0,len)+"way";

              else

              pStr = pStr + "ay";
              }
              return pStr;

              }

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by ahoway
                I have solved the first problem, now I need help with...

                1.Program repeats until user enters a null string
                2.Prompts the user to enter a string or just return to terminate

                here is my new code: Thank you in advance!
                #include "stdafx.h"
                #include <iostream>
                #include <string>
                #include <cstdlib>
                using namespace std;



                bool isVowel(char ch);
                string rotate(string pStr);
                string pigLatinString( string pStr);

                int main ()
                {

                string word,line;

                cout << "Enter a string or, blank to exit." <<endl;
                cout << "The program will output the words in Pig Latin."<<endl;
                getline(cin, line);


                cout << "The pig Latin form of " << line <<" is:\n";




                /* Process the input line word by word */


                while ( line.length() != 0)
                {
                //cout << "==> Enter line of text or enter return to stop input\n";




                int lineLen = line.length();
                nt wordLen = line.find(' ');
                if (wordLen > 0)
                {
                word = line.substr(0,w ordLen);
                line = line.substr(wor dLen+1);

                cout << pigLatinString( word) << " ";
                }
                else
                {
                cout << pigLatinString( line) <<"\n ";

                line = "";
                }
                }


                return 0;
                };



                ////////////////////////////////////////////////////////these are words starting with a vowel
                bool isVowel(char ch)
                {
                switch (ch)
                {
                case 'A': case 'E': case 'I': case 'O':case 'U':
                case 'a': case 'e':case 'i': case 'o': case 'u':
                return true;
                default: return false;
                };
                }///////////////////////////////////////////////////////////////end isVowel

                string rotate(string pStr)

                {
                string::size_ty pe len=pStr.length ();

                string rStr;

                rStr=pStr.subst r(1,len-1)+pStr[0];

                return rStr;
                }

                string pigLatinString( string pStr)

                {
                string::size_ty pe len;

                bool foundVowel;

                string::size_ty pe counter;

                if (isVowel(pStr[0]))

                pStr=pStr+"way" ;

                else
                {//These are words that didn't start with a vowel



                len=pStr.length ();
                foundVowel=fals e;



                if (isVowel(pStr[0]))

                {
                foundVowel=true ;
                }

                else

                pStr=rotate(pSt r);

                if (!foundVowel)

                pStr=pStr.subst r(0,len)+"way";

                else

                pStr = pStr + "ay";
                }
                return pStr;

                }
                I think your best bet is a while loop.

                Comment

                • ahoway
                  New Member
                  • Nov 2006
                  • 13

                  #9
                  Now I can get the loop to work, but my English to pig latin is gone?
                  // testpig.cpp : Defines the entry point for the console application.
                  //

                  #include "stdafx.h"
                  #include <iostream>
                  #include <string>
                  #include <cstdlib>
                  #include <cctype>
                  using namespace std;



                  bool isVowel(char ch);
                  string rotate(string pStr);
                  string pigLatinString( string pStr);

                  int main ()
                  {

                  string word,line;

                  cout << "Enter a string or, blank to exit." <<endl;
                  cout << "The program will output the words in Pig Latin."<<endl;
                  getline(cin, line);
                  cout<<endl;
                  cout << "The pig Latin form of " << line <<" is:\n";

                  while (line.length() !=0)
                  {

                  cout << "Enter a string or, blank to exit." <<endl;
                  cout << "The program will output the words in Pig Latin."<<endl;
                  getline(cin, line);
                  }


                  /* Process the input line word by word */


                  while ( line.length() != 0)
                  {
                  //cout << "==> Enter line of text or enter return to stop input\n";




                  int lineLen = line.length();
                  int wordLen = line.find(' ');

                  if (wordLen > 0)
                  {
                  word = line.substr(0,w ordLen);
                  line = line.substr(wor dLen+1);

                  cout << pigLatinString( word) << " ";
                  }
                  else
                  {
                  cout << pigLatinString( line) <<"\n ";

                  line = "";
                  }
                  }


                  return 0;
                  };



                  ////////////////////////////////////////////////////////these are words starting with a vowel
                  bool isVowel(char ch)
                  {
                  switch (ch)
                  {
                  case 'A': case 'E': case 'I': case 'O':case 'U':
                  case 'a': case 'e':case 'i': case 'o': case 'u':
                  return true;
                  default: return false;
                  };
                  }///////////////////////////////////////////////////////////////end isVowel

                  string rotate(string pStr)

                  {
                  string::size_ty pe len=pStr.length ();

                  string rStr;

                  rStr=pStr.subst r(1,len-1)+pStr[0];

                  return rStr;
                  }

                  string pigLatinString( string pStr)

                  {
                  string::size_ty pe len;

                  bool foundVowel;

                  string::size_ty pe counter;

                  if (isVowel(pStr[0]))

                  pStr=pStr+"way" ;

                  else
                  {//These are words that didn't start with a vowel



                  len=pStr.length ();
                  foundVowel=fals e;



                  if (isVowel(pStr[0]))

                  {
                  foundVowel=true ;
                  }

                  else

                  pStr=rotate(pSt r);

                  if (!foundVowel)

                  pStr=pStr.subst r(0,len)+"way";

                  else

                  pStr = pStr + "ay";
                  }
                  return pStr;

                  }

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Yeah, I'd use the while loop to test the input, and repeat output, then go on to the program, not do the program in a while loop...`

                    Comment

                    • ahoway
                      New Member
                      • Nov 2006
                      • 13

                      #11
                      Thanks for all the help. I changed my program around and this is what I came up with. It does exactly what the standards called for.

                      // piglatin_2.cpp : Defines the entry point for the console application.
                      //
                      // PigLatin.cpp : Defines the entry point for the console application.
                      //This program will take in a string of characters from a sentence. It will
                      //then look at each word and convert the word to Pig Latin. It will then
                      //put the sentence back toghther in Pig LAtin form. It has a loop inside it
                      // to continuously test for new strings in which the user types in. If there
                      //is nothing to be typed in and the NULL condition is satisfied the program will then
                      //break out of the loop..Does not look for punctuation

                      #include "stdafx.h"
                      #include <string>
                      #include <iostream>
                      using namespace std;


                      ///////////////////////////////////////////////////////////////////////
                      ///////////////////////FUNCTION LIST///////////////////////////////////////////////
                      bool isVowels(char x);
                      void Vowel(string changed);
                      void nonVowel(char begin, string end);
                      void changeLine (string line);
                      void printHeader();

                      ////////////////////////////////////////////////////////////////////////////////////
                      ////////////////////////////////////////////////////////////////////////////////////
                      ////////////////////BEGIN THE MAIN PROGRAM///////////////////////////////////////////


                      int main()
                      {

                      char ch;
                      string word, first, line;

                      printHeader();//prints my information
                      cout <<endl;
                      cout << "Enter a string or, blank to exit.\n";
                      cout << "This program will output the words in Pig Latin.\n";
                      cout <<endl;
                      getline(cin,lin e);//input a line of words
                      cout <<endl;
                      while (line.length()! = 0)//this loop was formed to keep the user able to
                      //input more sentences to be reconfigured into Pig Latin. If the loop recognizes
                      //the null space it will break out of the loop and end the program.
                      {
                      changeLine(line );// output of the new sentence in Pig Latin
                      cout<<endl;
                      cout<<endl;
                      cout << "Enter a string or, blank to exit.\n";
                      cout << "This program will output the words in Pig Latin.\n\n";
                      cout <<endl;
                      getline(cin,lin e);//input a line of words
                      }
                      return 0;
                      }
                      /////////////////////////////////////////////////////////////////////////////////
                      ////////////////////////////END OF MAIN//////////////////////////////////////////


                      bool isVowels(char x) // this is where all of the vowels in the words are found. It will
                      //read in the word, change the letter to loower case.
                      {
                      char low_case = tolower (x);
                      if (low_case == 'a' )
                      return true;
                      else if (low_case == 'e' )
                      return true;
                      else if ( low_case== 'i' )
                      return true;
                      else if (low_case == 'o' )
                      return true;
                      else if (low_case == 'u' )
                      return true;
                      else
                      return false;

                      }

                      void changeLine (string line)//this is where the strings are changed. As long as there is
                      //not a Null character, changeling will look at each word. It will look for the blank spaces
                      //between the words. One a word is found it is the look at to whether it has a vowel in it or not
                      // then it is rearranged into a string.
                      {
                      string word, first;
                      char ch;
                      while (line.length() !=0)//line consist of characters
                      {
                      int wordLen = line.find(' ');
                      if (wordLen >0)
                      {
                      word = line.substr(0,w ordLen);
                      first=word.subs tr(0,1);
                      ch = first[0];
                      string notFirst(word,1 );
                      if (isVowels (ch))// vowel found
                      {
                      Vowel(word);
                      }
                      else
                      {
                      nonVowel(ch,not First);//no vowel found
                      }
                      line = line.substr(wor dLen+1);
                      }
                      else
                      {
                      word = line;
                      ch = line[0];
                      string notFirst(word,1 );
                      if (isVowels (ch))
                      Vowel(word);
                      else
                      nonVowel(ch, notFirst);
                      line="";
                      }
                      }
                      }
                      void Vowel(string changed)//what this does is when the string of the new word is brought in
                      //it is changed due to Pig latin configuration for words that begin with a vowel and then added "way" to the end of the word.
                      //this new word is then processed out to the string to put back to sentence form.
                      {
                      string newWord;
                      newWord=changed +"way";
                      cout<<newWord<< " ";
                      }

                      void nonVowel(char begin, string end)//what this does is when the string of the new word is brought in
                      //it is changed due to Pig latin configuration for words that do not begin
                      //with a vowel and then added "ay" to the end of the word.this new word is then processed
                      //out to the string to put back to sentence form.
                      {
                      string newWord;
                      newWord=end+beg in+"ay";
                      cout<<newWord<< " ";
                      }
                      void printHeader()//This is the format for my header to print name and class info.
                      {
                      cout <<"\Anthony Howay"<<endl;
                      cout <<"csc 275 Prog 4-Pig Latin" <<endl;
                      cout <<"09 Nov. 2006"<<endl;

                      };

                      Comment

                      Working...