Pig Latin Strings Write a Program in C++. Task: Input various sentences as C-str
ID: 3823229 • Letter: P
Question
Pig Latin Strings Write a Program in C++. Task: Input various sentences as C-strings and perform 2 conversions on each sentence using C-string commands. 1. Reverse the order of the words, and print all letters in upper case; there will be no punctuation in this version. 2. Change the original line to pig latin; print this version all in lower case except start the pig latin sentence with a capital letter. The only punctuation will be a period at the end. The entire program will be written using only C-strings. Do not use string class objects for any part of it. As you process the conversions, assemble the results into complete strings, and print out only complete strings (don’t print a word at a time or a char at a time as you loop through a string). There are various ways that this program may be written. If you choose to use strtok, remember to make a copy of each word as it is extracted from the original string. Don't use the word pointer itself, or the loop will not work correctly. Pig Latin rules: * if a word begins with a vowel, append “-way” to the end: “eye” becomes “eye-way” * if a word starts with a consonant, identify the consonants up to the first vowel and move these consonants to the end of the word, then add “ay”: “slant” becomes “ant-slay” Input: Input can be interactive or read from a file. For the printed output, use at least these lines as input: I love Paris in the Spring time. Mary, my friend, had a lamb. Hello? Hello? Are you still there? John said, “Whoop Dee Doo!”, then skipped, hopped, and twirled. Output: Send the output to an external file. Print a header at the top of the page with at least your name, the date, and a title. Then print each input line, followed by the two conversions. Example: If the input line was: Hello? Hello? Are you still there? The output would be: Hello? Hello? Are you still there? THERE STILL YOU ARE HELLO HELLO Ello-hay ello-hay are-way ou-yay ill-stay ere-thay.
Explanation / Answer
PROGRAM CODE:
/*
* string.cpp
*
* Created on: 17-Apr-2017
* Author: kasturi
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void reverse(char* word)
{
for(int i=strlen(word)-1; i>=0; i--)
{
char sub[20] = "";
int index = 0;
while(word[i] != ' ' && i>=0 && word[i] != '')
{
if((word[i]<91 && word[i]>=65) || (word[i]<123 && word[i]>=97))
{
sub[index] = toupper(word[i]);
index++;
}
i--;
}
sub[index] = '';
for(int k=index-1; k>=0; k--)
cout<<sub[k];
cout<<" ";
}
cout<<endl;
}
bool isVowel(char character)
{
if(character == 'a' || character == 'A' || character == 'e' || character=='E' || character == 'i' || character == 'I'
|| character == 'o' || character == 'O' || character == 'u' || character == 'U')
{
return true;
}
else
return false;
}
void pigLatin(char *word)
{
int isFirst = 0;
for(int i=0; i<strlen(word); i++)
{
char sub[20] = "";
int index = 0;
while(word[i] != ' ' && i<strlen(word))
{
if((word[i]<91 && word[i]>=65) || (word[i]<123 && word[i]>=97))
{
sub[index] = tolower(word[i]);
index++;
}
i++;
}
if(isVowel(sub[0]))
{
cout<<sub<<"-way ";
}
else
{
char sub2[20] = "";
char sub3[20] = "";
int counter1 = 0;
int counter2 = 0;
while(!isVowel(sub[counter1]))
{
sub2[counter1] = sub[counter1];
counter1++;
}
while(counter1<index)
sub3[counter2++] = sub[counter1++];
if(isFirst == 0)
{
sub3[0] = toupper(sub3[0]);
isFirst = 1;
}
cout<<sub3<<"-"<<sub2<<"ay ";
}
}
cout<<endl;
}
int main()
{
char word[100] = "";
cout<<"Enter your string: ";
cin.getline(word,sizeof(word));
reverse(word);
pigLatin(word);
return 0;
}
OUTPUT:
Enter your string: Hello? Hello? Are you still there?
THERE STILL YOU ARE HELLO HELLO
Ello-hay ello-hay are-way ou-yay ill-stay ere-thay