[Solved-1 Solution] Convert sentence into Pig Latin in C++



Pig Latin - Data Model

  • The data model of Pig is fully nested. A Relation is the outermost structure of the Pig Latin data model. And it is a bag where -
    • A bag is a collection of tuples.
    • A tuple is an ordered set of fields.
    • A field is a piece of data.

Pig Latin - Statements

  • While processing data using Pig Latin, statements are the basic constructs.
  • These statements work with relations. They include expressions and schemas.
  • Every statement ends with a semicolon (;).
  • We will perform various operations using operators provided by Pig Latin, through statements.
  • Except LOAD and STORE, while performing all other operations, Pig Latin statements take a relation as input and produce another relation as output.
  • As soon as we enter a Load statement in the Grunt shell, its semantic checking will be carried out. To see the contents of the schema, you need to use the Dump operator. Only after performing the dump operation, the MapReduce job for loading the data into the file system will be carried out.

Example

  • The given statement loads the data into the apache pig
grunt> Student_data = LOAD 'student_data.txt' USING PigStorage(',')as 
   ( id:int, firstname:chararray, lastname:chararray, phone:chararray, city:chararra

Problem:

How to convert sentence into pig latin in C++ ?

Solution 1:

There are few random ideas for convert a sentence into pig latin

  1. To piglatinze a word: if first letter is a vowel, trivial; if not, find the first vowel. Split the string into two parts; output second part plus first part plus "ay".
  2. To find a consonant, just test for "not a vowel". Basically, we only need one is_vowel()function.
  3. Use std::string Anything else you'd be doing would not be learning C++.

A C++ program to convert a sentence to Pig Latin

#include <algorithm>	
#include <iostream>	
#include <string>	
	
using namespace std;	
	
// is_vowel returns true if the given	
// character is a vowel; false otherwise.	
bool is_vowel(char c)	
{	
    c = tolower(c);	
    return c == 'a'	
        || c == 'e'	
        || c == 'i'	
        || c == 'o'	
        || c == 'u'	
        || c == 'y'	
        ;	
}	
	
// to_piglatin translates a string to piglatin.	
//	
// For words that begin with consonant sounds,	
// the initial consonant or consonant cluster	
// is moved to the end of the word, and "ay"	
// is added.	
// For words that begin with vowel sounds or	
// silent letter, you just add "way".	
string to_piglatin(const string& str)	
{	
    auto iter = find_if(str.begin(), str.end(), is_vowel);	
    if (iter == str.begin()) {	
        return str + "way";	
    }	
    else if (iter != str.end()) {	
        string ingstray(iter, str.end());	
        ingstray.append(str.begin(), iter);	
        return ingstray + "ay";	
    }	
    return str;	
}	
	
int main(int argc, char* argv[])	
{	
    string str;	
    for (int i = 1; i < argc; i++) {	
        str = argv[i];	
        cout << to_piglatin(str) << " ";	
    }	
    cout << endl;	
    return 0;	
}	

Related Searches to Convert sentence into Pig Latin in C++