Howdy, any help/ instruction would be greatly appreciated! Here it is in Java, c
ID: 3923178 • Letter: H
Question
Howdy, any help/ instruction would be greatly appreciated! Here it is in Java, could you help me convert it to c++? There is a children's "secret language" called "Pig Latin" which supposedly allows conversation the uninformed (such as parents) cannot understand. The rules are: o If a word begins with a vowel (aeiou) then append "way" to the word. For example, "Aggie" becomes "Aggieway". o 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 from a file named in.txt and writes the words in Pig Latin to a file named out.txt. So if in.txt contains the words I love Reveille This is the street then the words Iway ovelay eveilleRay isThay isway ethay eetstray will be written to out.txt.
Explanation / Answer
Answer
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include<fstream.h>
#include <stdlib.h>
//using namespace std;
int isVowel(char ch)
{ if(ch==' '){}
else
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
void main()
{
char word[100];
clrscr();
fstream in;
fstream out;
in.open("in.txt",ios::in);
out.open("out.txt",ios::out);
if(in.fail())
{
cout<<"Input file failed to open ";
getch();
}
else if(out.fail())
{
cout<<"Output File failed to open ";
getch();
}
else
{
while(in)
{
in.getline(word,100); // delim defaults to ' '
// cout<<"Enter a word : ";
//gets(word);
char normalWord[100];
strcpy(normalWord,word);
char ch = word[0]; // first character
if(isVowel(ch))// if word starts with an vowel, append "way" to the word.
{
strcat(word,"way");
}
else
{
int vowelIndex ;//= strlen(word);
for(int i = 0; i < strlen(word); ++i)
{
if(isVowel(word[i]))
{
vowelIndex = i;
break;
}
}
char tempword[100];
int k=0;
for(int j=vowelIndex;j<strlen(word);j++)
{
if(word[j]=='')
{
}
else
{
tempword[k]=word[j];
k++;
}
}
strcpy(word,tempword);
// free(tempword);
char tempword2[100];
k=0;
int l;
for(l=0;l<vowelIndex;l++)
{
if(normalWord[l]=='')
{
}
else
{
tempword2[k]=normalWord[l];
k++;
}
}
tempword2[l]='';
strcat(word,tempword2);
// free(tempword2);// tempword2[0]='';
strcat(word,"ay");
memset(tempword, 0, 100);
memset(tempword2, 0, 100);
}
out<<word<<" ";
memset(word, 0, 100);
memset(normalWord, 0, 100);
//cout<<" Pig-latin of "<<normalWord <<" is "<<word;
}
}
in.close();
out.close();
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.