diff --git a/DSA Problems 1/77_convert_to_piglatin.cpp b/DSA Problems 1/77_convert_to_piglatin.cpp new file mode 100644 index 0000000..bbeb8dc --- /dev/null +++ b/DSA Problems 1/77_convert_to_piglatin.cpp @@ -0,0 +1,42 @@ +#include +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; +}