Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions DSA Problems 1/77_convert_to_piglatin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <bits/stdc++.h>
using namespace std;
//A Pig Latin is an encrypted word in English, which is generated by doing the following alterations:
//The first vowel occurring in the input word is placed at the start of the new word along with the remaining
//alphabet of it. The alphabet is present before the first vowel is shifted,
//at the end of the new word it is followed by “ay”.
bool isVowel(char c)
{
return (c == 'A' || c == 'E' || c == 'I' ||
c == 'O' || c == 'U' || c == 'a' ||
c == 'e' || c == 'i' || c == 'o' ||
c == 'u');
}

string pigLatin(string s)
{
int len = s.length();
int index = -1;
for (int i = 0; i < len; i++) {
if (isVowel(s[i])) {
index = i;
break;
}
}

// Pig Latin is possible only if vowels
// is present
if (index == -1)
return "-1";

return s.substr(index) + s.substr(0, index) + "ay";
}

// Driver code
int main()
{
string str = pigLatin("graphic");
if (str == "-1")
cout << "No vowels found. Pig Latin not possible";
else
cout << str;
}