In: Computer Science
7. Write a function that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY.
As you have not mentioned the language I am writing function in c++;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
//This function returns PigLatin form of string given as
argument
string convertToPigLatin(string str){
//Extracting words from s
vector<string> bag;
string word = "";
for (auto x : str)
{ //converting each word to PigLatin
if (x == ' ')
{
word+=word[0];
word+="AY";
word.erase(0,1);
bag.push_back(word);
word =
"";
}
else
{
word =
word + x;
}
}
//Extracting out the last word
word+=word[0];
word+="AY";
word.erase(0,1);
bag.push_back(word);
string ans="";
for(int i=0;i<bag.size();i++){
ans+=bag[i];
if(i!=bag.size()-1)
ans+=" ";
}
return ans;
}
int main(){
string s;
getline(cin,s);
cin>>s;
cout<<convertToPigLatin(s);
}