In this tutorial, we will explore the process of encoding a sentence into Pig Latin using C++ with multiple illustrations.
Pig Latin encryption is a technique to encode normal sentences into abnormal sentences. The rules to convert a particular sentence to Pig Latin are:
- First, divide the sentence into words.
- Next, remove the first character from each word and add that character to the end of the word.
- After that, add string "ay" to the end of the word.
- Finally, combine all the words back into a sentence.
Example:
Original sentence: have a nice day
Encrypted sentence: avehay aay icenay ayday
Explanation: The sentence is separated into individual words for modification. Each word is transformed by moving the first letter to the end followed by adding 'ay' at the end. The adjusted words (have, a, nice, day) become (avehay, aay, icenay, ayday) respectively. Finally, the updated words are combined to reconstruct the modified sentence.
This cryptographic method finds applications in everyday scenarios such as safeguarding communication privacy. It can be utilized in programming to obscure data. This encryption technique is also employed in the practice of steganography.
Example 1:
Let's consider a C++ code snippet that transforms a provided sentence into a pig latin equivalent.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
string modifyWord(const string& word) {
if (word.empty())
return "";
char firstChar = word[0];
string modifiedWord = word.substr(1);
modifiedWord += firstChar;
modifiedWord += "ay";
return modifiedWord;
}
int main() {
string sentence;
cout << "Enter a sentence: ";
getline(cin, sentence);
vector<string> words;
stringstream ss(sentence);
string word;
while (ss >> word) {
words.push_back(word);
}
for (string& w : words) {
w = modifyWord(w);
}
string modifiedSentence;
for (const string& w : words) {
modifiedSentence += w + " ";
}
cout << "Modified Sentence: " << modifiedSentence << endl;
return 0;
}
Output:
Explanation:
This software contains a pair of functions named "main" and "adjustWord" within the main function. The user inputs a sentence, which is then stored in a vector called "words" to hold individual words. Subsequently, a string stream is initialized to break down the sentence into tokens, with each tokenized word being appended to the "words" vector. An iteration is performed using a for loop to traverse through all words, invoking the "adjustWord" function with each word as an argument. This function alters the word and returns the modified version, which is then consolidated and saved in "modifiedSentence". Finally, the adjusted sentence is displayed.
The "adjustTerm" function accepts a term as an argument. In case the term is an empty string, it will be directly returned as an empty string. Otherwise, the initial character of the term will be saved in a variable named "initialLetter", then the first character will be eliminated from the original term. The initial letter will be appended at the end of the term. Subsequently, a unique string "ay" will be attached to the adjusted term before sending it back to the primary function.
Example 2:
Let's consider a C++ program that converts a given sentence into Pig Latin.
#include <iostream>
#include <string>
using namespace std;
string encryptToPigLatin(const string& input) {
string encryptedString = "";
for (int i = 0; i < input.length(); i++) {
int wordStart = i;
if (i >= input.length())
break;
while (i < input.length() && input[i] != ' ')
i++;
if (encryptedString.length() == 0) {
encryptedString.append(input.substr(wordStart + 1, i - wordStart - 1) + input[wordStart] + "ay");
} else {
encryptedString.append(" " + input.substr(wordStart + 1, i - wordStart - 1) + input[wordStart] + "ay");
}
}
return encryptedString;
}
int main() {
string inputString;
cout << "Enter a sentence: ";
getline(cin, inputString);
string encryptedString = encryptToPigLatin(inputString);
cout << "The Pig Latin encrypted string is: " << encryptedString;
return 0;
}
Output:
Explanation:
This software comprises two functions: "main" and "encryptToPigLatin". Within the main function, a user-input sentence is collected. Subsequently, this sentence is transferred to the "encryptToPigLatin" function, accompanied by a parameter. The function then delivers back the altered string in pig Latin format, which is then displayed.
The function "encryptToPigLatin" accepts a sentence as its argument. Within this function, a new string variable named "encryptedString" is created and set to empty. A for loop is implemented to iterate through each character in the sentence. If the index "i" exceeds or equals the length of the input, the loop is exited. The function then determines the end of the current word. In the case of the first word, the initial character is moved to the end of the word and "ay" is added. However, for subsequent words, a space is appended before adding "ay". Ultimately, the modified string is returned to the main program.