Question

Pig Latin program using Linked Lists C++

Write a program that prompts the user to input a string and then outputs the string in the pig Latin form. The rules for converting a string into pig Latin form are asfollows:

a. If the string begins with a vowel, add the string "-way" at the end of the string. for example, the pig Latin form of the string "eye" is "eye-way".

b. If the string does not begin with a vowel, first ass "-" at the end of the string. Then rotate the string one character at a time; that is, move the first characterof the string to the end of the string until the first character of the string becomes a vowel. The add the string "ay" at the end. For example, the pig Latin form ofthe string "There" is "ere-Thay".

c. Strings such as "by" contain no vowels. In cases like this, the letter y can be considered a vowel. So, far this program the vowels are a, e, i, o, u, y. Therefore,the pig Latin form of "by" is "y-bay".

d. Strings such as "1234" contain no vowels. The pig Latin form of the string "1234" is "1234-way". That is, the pig Latin form of a string that has no vowels in it isthe string followed by the string "-way".

Your program must store the characters of a string into a linked list and user the function rotate (function to remove the first node of a linked list and put it atthe end of the linked list) to rotate the string.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Dear....

// Latin.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "stdafx.h"

#include <string>

#include <iostream>

using namespace std;

bool isVowels(char x);

void Vowel(string changed);

void nonVowel(char begin, string end);

void changeLine (string line);

void printHeader();

int main()

{

char ch;

string word, first, line;

printHeader();

cout <<endl;

cout << "Enter a string.n";

cout << "String shows in Pig Latin.n";

cout <<endl;

getline(cin,line);

cout <<endl;

while (line.length()!= 0)

{

changeLine(line);

cout<<endl;

cout<<endl;

cout << "Enter a string .n";

cout << "String shows in Pig Latin.nn";

cout <<endl;

getline(cin,line);

}

return 0;

}

bool isVowels(char x)

{

char low_case = tolower (x);

if (low_case == 'a' )

return true;

else if (low_case == 'e' )

return true;

else if ( low_case== 'i' )

return true;

else if (low_case == 'o' )

return true;

else if (low_case == 'u' )

return true;

else

return false;

}

void changeLine (string line)

{

string word, first;

char ch;

while (line.length() !=0)

{

int wordLen = line.find(' ');

if (wordLen >0)

{

word = line.substr(0,wordLen);

first=word.substr(0,1);

ch = first[0];

string notFirst(word,1);

if (isVowels (ch))

{

Vowel(word);

}

else

{

nonVowel(ch,notFirst);

}

line = line.substr(wordLen+1);

}

else

{

word = line;

ch = line[0];

string notFirst(word,1);

if (isVowels (ch))

Vowel(word);

else

nonVowel(ch, notFirst);

line="";

}

}

}

void Vowel(string changed)

{

string newWord;

newWord=changed+"way";

cout<<newWord<<" ";

}

void nonVowel(char begin, string end)

{

string newWord;

newWord=end+begin+"ay";

cout<<newWord<<" ";

};

answered by: Suzi
Add a comment
Answer #2
it will going to help u defiantly

http://books.google.co.in/books?id=4Fn_P7tdOZgC&pg=PA494&lpg=PA494&dq=Write+a+program+that+prompts+the+user+to+input+a+string+and+then+outputs+the+string+in+the+pig+Latin+form.&source=bl&ots=gTsI2NlttG&sig=yEt4C9IEjJUAm3ri5JEROSJLDz8&hl=en&ei=mMO5TsCQDorxrQebwfCxBg&sa=X&oi=book_result&ct=result&resnum=5&ved=0CDwQ6AEwBA#v=onepage&q=Write%20a%20program%20that%20prompts%20the%20user%20to%20input%20a%20string%20and%20then%20outputs%20the%20string%20in%20the%20pig%20Latin%20form.&f=false
Add a comment
Answer #3


#include <iostream>
#include <string>
using namespace std;
#define yes 1 //allows us to use integers as a kind of pseudo-boolean
#define no 2 //see above


int main() {
string instr; //input string, converted to the c-string input
char * input; //the c-string for input, provides manipulation
int strlen; //string legnth
int controller=0; //control variable for the loop
char firstchar; //first character of word
int leadingspace=no; //indicates that there is a leading space before the letter tested
int exception=no; //indicates end of string
cout << "Input string to be translated into Pig Latin: "; //output
getline (cin, instr); //inputs the string
instr += '40'; //adds a space to the end of the string.
strlen=instr.size()+1; //gets the legnth of the string that we should use for the array (remember, add 1 for null terminator)
input=new char[strlen]; //allocates memory for the array of size string legnth.
memcpy (input, instr.c_str(), strlen); //copies string to char array of legnth strlen
if (input[0]=='40') { //if the first char is a space
for (controller++; input[controller]=='40'; controller++); //creates an empty loop that will increment the controller variable until thefirst letter
}
firstchar=input[controller]; // now we'll set firstchar to whatever controller happens to be.
controller++; //go to the next letter
for (;controller<strlen;controller++) { //creates the output loop
if (leadingspace==yes) { //assign the first char of a word if the leadingspace is true
firstchar=input[controller-1];
}
if (input[controller]=='40') { //if a space
cout << firstchar << "ay "; //output the firstletter plus the ay of piglatin
for (;input[controller]=='40'; ++controller) { //a loop (similar to the loop at line 23
if (input[controller]=='') { //check for end of string
exception=yes;
break; //jump out of test loop
}
}
if (exception==yes) break; //jump out of main loop
leadingspace=yes; //change leading space so that the first if statement (setting firstchar) is evaluated
continue; //skip over output (to prevent output of the first letter of a word) and leadingspace reassignment
}
cout << input[controller]; //if it wasn't skipped over by continue will output the letter
leadingspace=no; //again, if not skipped over, will set leading space to no so that it doesn't reassign firstchar next time
}
delete[] input; //now that we've outputed our data, we can delete the c-string to free up memory
cout << "nTo Exit: Press any key, then enter: "; //output
char exitchar; //a simple portable way to exit (for better way see pinned thread at top of C++ forum)
cin >> exitchar;
return 0; //if nothing went wrong, it'll return 0 and end the loop
}
//DONE


OR

Header file
#ifndef PIGLATIN_H
#define PIGLATIN_H

#include <iostream>

using namespace std;

cont int ARRAY_START = 0;

// This function determines wheter a word begins with a consonant, vowel
//or qu.
//PRE: ifstream must be declared and connected.
//POST: The function returns an interger value the represents what type
//that the word is. 1 = vowel; 2 = qu; 3 = consonant
int WordType (const string word, cont int START);

//This function converts a word that starts with a vowel into a Pig-latin
//word.
//PRE: None
//Post: The word given is passed by reference and will be changed.
void vowel_PLatin (string&word);

//This function converts a word that starts with quinto a Pig-latin word.
//PRE: None
//Post: The word given is passed by reference and will be changed.
void qu_PLatin (string&word);

//This function converts a word that sarts with a consonant into a Pig-latin
//word.
//PRE: None
//Post: The word given is passed by reference and will be changed.
void consonant_PLatin (string&word);

#endif


Main CPP
#include "piglatin.h"
#include <fstream>

int main ()
{
string filename;
string word;
int type;

ifstream in;
ofstream out;

cout << "Enter the Name of the Input File: ";
cin >> filename;
in.open(filename.c_str());

cout << "Enter the Name of the Output File: ";
cin >> filename;
out.open(filename.c_str());

while (in>>word)
{
type = WordType(word, ARRAY_START);

if (type == 1)
word = vowel_PLatin(word);
else if (type == 2)
word = qu_PLatin (word);
else
consonant_PLatin (word);

out << word;
}

return 0;
}

Sub-CPP
#include "piglatin.h"

int WordType (const string word, cont int START)
{
int i =3;
if (word[START] == 'a' &&word[START] == 'e' &&word[START] == 'i'
&&word[START] == 'o' &&word[START] == 'u')
int i = 1;
else if(word [i] =='q' &&word[i + 1] == 'u')
int i = 2;

return i;
}

void vowel_PLatin (string&word)
{
word.append("-hay");

return 0;
}

void qu_PLatin (string&word)
{
word.replace(0,2,"");
word.append("-quay");

return 0;
}

void consonant_PLatin (string&word)
{
string temp;
bool consonant = true;
int i = 0;
temp = word[i];
do
{
i++;
if (word[i] == 'a' &&word[i] == 'e' &&word[i] == 'i'
&&word[i] == 'o' &&word[i] == 'u')
{
temp.append(i, 1, word[i]);
}
else
consonant = false;
} while (consonant == true);
word.replace(0,1, "");
word.append("-");
word.append(temp);
word.append("ay");

return 0;
}

answered by: ubaidullah
Add a comment
Know the answer?
Add Answer to:
Pig Latin program using Linked Lists C++
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /** * Returns the result of converting s to "Pig Latin". Convert a string s to...

    /** * Returns the result of converting s to "Pig Latin". Convert a string s to Pig Latin by using the following rules: * * (1) If s contains no vowels, do nothing to it. * * (2) Otherwise, if s starts with a vowel, append "way" to the end. * * (3) Otherwise, move everything up to (but not including) the first vowel to the end and add "ay". * * For example, "hello" converts to "ellohay", "small" converts...

  • In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need...

    In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need to write a program that reads an input string representing a sentence, and convert it into pig latin. We'll be using two simple rules of pig latin: 1. If the word begins with a consonant then take all the letters up until the first vowel and put them at the end and then add "ay" at the end. 2. If the word begins with...

  • use C++ Pig Latin Background Pig latin is a "language” where you take the regular English...

    use C++ Pig Latin Background Pig latin is a "language” where you take the regular English Word, remove the first letter, place it on the end of the word, and then append "ay" to the end of the word. Pig Latin = Igpay Atinlay Functionality 1) Prompt the user to enter any input string (I will test it with multiple words). 2) After receiving and storing (if needed) their input, change their words to Pig Latin by placing the first...

  • In C please Pig latin has two very simple rules: If a word starts with a...

    In C please Pig latin has two very simple rules: If a word starts with a consonant move the first letter(s) of the word, till you reach a vowel, to the end of the word and add "ay" to the end. have ➞ avehay cram ➞ amcray take ➞ aketay cat ➞ atcay shrimp ➞ impshray trebuchet ➞ ebuchettray If a word starts with a vowel add "yay" to the end of the word. ate ➞ ateyay apple ➞ appleyay...

  • Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin....

    Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin.” The rules used by Pig Latin are as follows: • If a word begins with a vowel, just as "yay" to the end. For example, "out" is translated into "outyay". • If it begins with a consonant, then we take all consonants before...

  • There is a children's "secret language" called "Pig Latin" which supposedly allows conversation the uninformed (such...

    There is a children's "secret language" called "Pig Latin" which supposedly allows conversation the uninformed (such as parents) cannot understand. The rules are: If a word begins with a vowel (aeiou) then append "way" to the word. For example, "Aggie" becomes "Aggieway". Otherwise, remove the consecutive non-vowels from the beginning of the word, append them to the end of the word, and append "ay" to the word. For example, "whoop" becomes "oopwhay". Write a program named hw5pr1.cpp which reads words...

  • Design and code a SWING GUI to translate text entered in English into Pig Latin. You...

    Design and code a SWING GUI to translate text entered in English into Pig Latin. You can assume that the sentence contains no punctuation. The rules for Pig Latin are as follows: For words that begin with consonants, move the leading consonant to the end of the word and add “ay”. Thus, “ball” becomes “allbay”; “button” becomes “uttonbay”; and so forth. For words that begin with vowels, add “way’ to the end of the word. Thus, “all” becomes “allway”; “one”...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line wh...

    String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline(), converts the string into Ubbi dubbi and displays the translated sentence. Ubbi dubbi works by adding ub before each vowel sound in a syllable. For example, hello becomes hubellubo. The word speak has the vowel sound ea, so in Ubbi dubbi it becomes spubeak....

  • Take the following C++ code and add to it the following: Write a program that reads...

    Take the following C++ code and add to it the following: Write a program that reads a string and outputs the number of times each lowercase vowel appears in it. Your program must contain a function with one of its parameters as a char variable, and if the character is a vowel, it increments that vowel's count. #include<iostream> #include<string> using namespace std; int countVowel(char, char); int main(void) { string str1; int countA = 0; int countE = 0; int countI...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT