Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>
#include<string>
#include<fstream>
#include<conio.h>
using namespace std;


 
int main(){
string sSentence;
ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,sSentence) )
    {
      cout << sSentence << '\n';
    }
    myfile.close();
  }
	else cout << "Unable to open file"; 

int iLen = sSentence.length();
int iBlank = sSentence.find(" ");

        int o=0;       
		string sSen = sSentence.substr(o,iBlank);          
		         
	for(;o<iLen;o++){
		
			if( isalpha(sSentence[o])){ 
			
				int iLength = sSen.length();
				int iVowel = sSen.find_first_of("aeiouAEIOU");
				string adSent = sSen.substr(o,iVowel);
				string addSent = sSen.substr(iVowel,iBlank);
				string pig = addSent+adSent;
				string latin = pig.insert(iBlank,"ay");
				cout << latin;
				
				}
		
			else{
				cout<< sSentence[o];
				}
				
			
				
	} 
	
	
return 0;
}



in this code. im suppose to translate a word to Piglatin.
inside the .txt file the message is:
"i love you"

the program is suppose to print
"iay ovelay ouyay"

so it finds the first vowel in the word then all the letters before the vowel will be transfered to the end of the word then add the letters "ay".


in my program the loop there works only for the first word that it can find.

my problem is i need to move my index to the last letter of the word. so that when I increment the index of the string array. i would get the space and print it. how can I assign o to the location of the last letter of the word?

there is an error if i put
o = iBlank-1; 

at the end of the if condition. i need to jump to the next word and find the next blank space in the sentence.

pls help thank you :))
Posted

It would be better to create a subroutine that accepts a string, converts it to its piglatin form, and return the converted string. Your main function then only needs to split the input string into tokens, and pass each token to the converter in turn. In this way the converter does not need to be concerned about the size of each word, or where it fits in the overall sentence. The key, as always, is to try to break your problem down into small easily managed components, rather than trying to do everything in one serial process.
 
Share this answer
 
You have three important indices for each word, namely
  • The starting letter, let's name it start.
  • The index of the vowel, let's name it vowel
  • The index of the blank (or of the pass-the-end of the string), let's call it end


Once you realize that the implementation is straightforward:
C++
void piglatin(const string & sSentence)
{
  size_t start, vowel, end;
  start = 0;
  do
  {
    end = sSentence.find(' ', start);
    vowel = sSentence.find_first_of("aeiouAEIOU", start);
    cout << sSentence.substr(vowel, end-vowel) << sSentence.substr(start, vowel-start) << "ay ";
    start = end + 1;
  } while (end != string::npos);
  cout << endl;
}
 
Share this answer
 
Comments
Reuben Cabrera 21-Feb-15 23:55pm    
after printing the word i need to find the next word. so i can get the position of the next blank space. so i need to update the value of o so that i skip evaluating all the other letters of the word.

so your function does convert i --> iay but after the execution of the function . the loop must skip the rest of the letters and go directly to the position of blank space. so i can find the next word.
CPallini 22-Feb-15 6:49am    
My function transforms 'i love you' into 'iay ovelay ouyay'. This is your requirement, isn't it?
Reuben Cabrera 22-Feb-15 9:56am    
ah ok when I used it on my program it didnt change the rest of the word. maybe i place your algo in the wrong place. sorry bout that
Reuben Cabrera 22-Feb-15 9:56am    
thanks btw. :)
Reuben Cabrera 22-Feb-15 10:04am    
why does your function use const string & sSentence.
why use const string &??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900