Im writing a c++ program to translate a sentence to pig latin. But I keep getting an error where it only prints out the "ay" part. Not sure where I'm wrong here.
string piglatin(string);
string substr(string, int&);
int main () //begin main
{
string input;
cout<<"Enter a sentence to convert: ";
getline(cin, input);
cout<<"Converted to Pig Latin:
"<<piglatin(input)<<endl<<endl;
//cout << "Search for another value? (Y/N)"; //to convert
again
system("pause");
return 0;
}
string piglatin(string input) //piglatin function
{
int len,
counter = 0,
start = 0,
stop = 0;
string word =" ",
newstring =" ";
do
{
word = substr(input, start); //translate
start++;
newstring=newstring+word+" ";
}
while(start<input.length());
return newstring;
}
string substr(string s,int&n)
{
char word[50]="", suffix[2];
int i=0;
suffix[0]=s[n];
suffix[1]='\0';//NULL
n++;
while(s[n]!=' '&&s[n]!='\0') //while(s[n]!='
'&&s[n]!='\0')
{
word[i]=s[n];
n++;
i++;
}
strcpy(word,suffix); //add the suffix
strcpy(word,"ay");//add ay to it
return word;
The problem is in the function string substr():- Wrong usage of strcpy() method:
So let me explain what the strcpy() method does:
Suppose a code:
string s1 = "Hello",s2 = "World";
strcpy(s1,s2); //s1 is the target variable, s2 is the variable
whose string will be copied to s1
cout<<s1<<endl;
Then here, the output will be:
World
So, the string s2 is copied to s1 and not added to s1.
What you want to achieve is that the string s2 gets added at the
end of s1 in this case, and in your code in the string substr
function. So instead of using strcpy(), you will have to use
strcat() method.
Now, let me demonstrate to you the usage of strcat using the above
example:
Suppose a code:
string s1 = "Hello ",s2 = "World";
strcat(s1,s2); //s1 is the target variable, s2 is the variable
whose string will be added to s1
cout<<s1<<endl;
Here,the output will be:- Hello World
which is what you want in your code, so your revised code is as
follows which works:
CODE:
#include<iostream>
#include<string.h>
using namespace std;
string piglatin(string s);
string substr(string a, int&n);
int main () //begin main
{
string input;
cout<<"Enter a sentence to convert: ";
getline(cin, input);
cout<<"Converted to Pig Latin:
"<<piglatin(input)<<endl<<endl;
//cout << "Search for another value? (Y/N)"; //to convert
again
system("pause");
return 0;
}
string piglatin(string input) //piglatin function
{
int len,
counter = 0,
start = 0,
stop = 0;
string word =" ",
newstring =" ";
do
{
word = substr(input, start); //translate
start++;
newstring=newstring+word+" ";
}
while(start<input.length());
return newstring;
}
string substr(string s,int&n)
{
char word[50]="", suffix[2];
int i=0;
suffix[0]=s[n];
suffix[1]='\0';//NULL
n++;
while(s[n]!=' '&&s[n]!='\0') //while(s[n]!='
'&&s[n]!='\0')
{
word[i]=s[n];
n++;
i++;
}
strcat(word,suffix); //add the suffix
strcat(word,"ay");//add ay to it
return word;
}
_________________________________________________________
CODE IMAGE FOR INDENTATION:

______________________________________________________________
OUTPUT:

___________________________________________________________
I hope I was able to solve your problem. If you have any doubts
feel free to get it clarified in the comments section.
Thank You!
Im writing a c++ program to translate a sentence to pig latin. But I keep getting...
JAVA PROGRAMMING LANGUAGE!!!! Pig Latin Write a program that reads a sentence as input and converts each word to "Pig Latin". In one version of Pig Latin, you convert a word by removing the first letter, placing that letter at the end of the word, and then appending "ay" to the word. Here is an example: English: I SLEPT MOST OF THE NIGHT Pig Latin : TAY LEPTSAY OSTMAY FOÀY HETAY 1GHTNAY make sure that there are two...
Please use PYTHON3 to Create a program that translates English into Pig Latin. The function will take the first letter of each word in the sentence only if it’s a not a vowel, and place it at the end of the word followed by “ay”. Your program must be case insensitive. You must write the function translate() that takes in a single English word and returns its Pig Latin translation. Remember translate must take only one word as input. #############################################################...
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 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...
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”...
im writing a c++ code and getting stuck on two parts. The first part, when I go to write the customer order out to the file, it writes everything but the price out right. I'll get a weird number like 5.95828e-039 or nan. When I printout to the console it works fine so not sure what's wrong. Second After I write the order out to the file, I then have to go in and read the file and calculate the...
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....
I need MIPS code for this program Translate the following C++ program to MIPS assembly program (Please explain each instruction of your code by a comment and submit a .asm file) // Example program #include <iostream> #include <string> using namespace std; int main() { const int ADULT_CHOICE= 1, CHILD_CHOICE= 2, SENIOR_CHOICE= 3, QUIT_CHOICE= 4, ADULT = 250, CHILD = 200, SENIOR = 350; int choice, months; int charges = 0; do { cout <<"\n\t\tHealth Club Membership Menu\n\n" <<"1. Standard Adult...
How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){ string seat, flight, book = "y"; int int_flight, p = 0, j = 0; int seat_number,...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...