Can someone add appropriate comments for this code? #include<iostream> #include<
ID: 3861666 • Letter: C
Question
Can someone add appropriate comments for this code?
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
/**
*
*/
bool isVowel(char ch);
/**
* Removes the first character of the string, and places it at the
* end of the string.
* @param pString the word to be rotated.
* @return the new reordered string
*/
string rotate(string pStr);
/**
*
*/
string pigLatinString(string pStr);
int main()
{
string str;
cout << "Enter a word: ";
if (cin.peek() == ' ') {
cin.ignore(1, ' ');
}
getline(cin, str);
cout << endl;
// Output the text as pig Latin
cout << "The pig Latin form of " << str << " is: "
<< pigLatinString(str) << endl;
return 0;
}
bool isVowel(char ch)
{
switch (ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
default:
return false;
}
}
string rotate(string pStr)
{
string::size_type len = pStr.length();
string rStr;
rStr = pStr.substr(1, len - 1) + pStr[0];
return rStr;
}
string pigLatinString(string pStr)
{
string::size_type len;
bool foundVowel;
string::size_type counter;
if (isVowel(pStr[0]))
pStr = pStr + "-way";
else
{
pStr = pStr + '-';
pStr = rotate(pStr);
len = pStr.length();
foundVowel = false;
for (counter = 1; counter < len - 1; counter++)
{
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else
pStr = rotate(pStr);
}
if (!foundVowel)
pStr = pStr.substr(1, len) + "-way";
else
pStr = pStr + "ay";
}
return pStr;
}
Explanation / Answer
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
/**
*
*/
bool isVowel(char ch);//method to check whether ch is vowel or not..
//if yes returns true//other wise false
/**
* Removes the first character of the string, and places it at the
* end of the string.
* @param pString the word to be rotated.
* @return the new reordered string
*/
string rotate(string pStr);
/**
*
*/
string pigLatinString(string pStr);
int main()
{
string str;
cout << "Enter a word: ";//prompting user to enter a word
if (cin.peek() == ' ') //returns the next character in the input sequence
{
cin.ignore(1, ' ');//if it is line then ignoring it
}
getline(cin, str);//reading line and storing in str
cout << endl;
// Output the text as pig Latin
cout << "The pig Latin form of " << str << " is: "
<< pigLatinString(str) << endl;//printing output
return 0;
}
bool isVowel(ch)//checking whether vowel or nor
{
switch (ch)//switch statement
{//for each vowel,upper and lower...defining cases to perform actions
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;//after all cases this will execute
default:
return false;
}
}
string rotate(string pStr)
{
string::size_type len = pStr.length();//finding string length
string rStr;//new variable declaration
rStr = pStr.substr(1, len - 1) + pStr[0];//removing first character from pstr,and adding at the end, and assigned to rstr
return rStr;//returning rotated string
}
string pigLatinString(string pStr)
{
string::size_type len;//variable declaration
bool foundVowel;
string::size_type counter;
if (isVowel(pStr[0]))//if first char in pStr is vowel then
pStr = pStr + "-way";//adding -way to pStr
else//if it is not vowel
{
pStr = pStr + '-';//then adding - , at end of pStr
pStr = rotate(pStr);//rotating pStr
len = pStr.length();//finding length of new pStr
foundVowel = false;
for (counter = 1; counter < len - 1; counter++)
{
if (isVowel(pStr[0]))//checking first char in pstr is vowel or not
{
foundVowel = true;//if vowel
break;
}
else
pStr = rotate(pStr);//rotating..pStr
}
if (!foundVowel)
pStr = pStr.substr(1, len) + "-way";
else//if it is vvowel
pStr = pStr + "ay";
}
return pStr;
}