Strings question

closed account (LN3RX9L8)
This is my first program time writing code with strings and I would like how I would do number 5?

1 Ask the user to enter the name of a file
2 The file should contain one word per line
3 Open the file and read in the words one at a time. For each word
4 Print the word
5 Covert to Pig Latin using the rules
-If the word starts with a vowel, append “ay” to the end of the word
-If the word starts with a consonant, move the first consonant to the end of the word, then append “ay"

Here is my following code: ( I am not sure if it is correct)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;


int main()

{
	ifstream infile;
	string file_name;
	string line;
    
	cout<<"Welcome to the Pig Latin Translator!"<<endl;
	cout<<endl;
    
    
	cout<< "Please enter a file name: "<<endl;
	cin>> file_name;
    
	infile.open(file_name);
    
	if(infile.fail())
	{
		cout<<"Sorry, could not open input file!\n";
        
		exit(1);
	}
    
	while(infile.good())
        
	{
        getline(infile,line);
		cout<<line<<endl;
	}
}
how I would do number 5?

...

5 Covert to Pig Latin using the rules
-If the word starts with a vowel, append “ay” to the end of the word
-If the word starts with a consonant, move the first consonant to the end of the word, then append “ay"


You can use the [] operator on strings to access a particular character. For instance,

1
2
std::string s = "abcdef";
char aCharacter = s[0]; //here, aCharacter will store 'a' 


http://www.cplusplus.com/reference/string/string/operator[]/

For modularity, you can define a function that takes in a char and returns true if it is a vowel and false otherwise.

For appending to a string, strings have the append function (or you could use their += operator)

http://www.cplusplus.com/reference/string/string/operator+=/
http://www.cplusplus.com/reference/string/string/append/

For the case where the string starts with a consonant, you can grab the rest of the string using the substr function.

http://www.cplusplus.com/reference/string/string/substr/
closed account (LN3RX9L8)
@shacktar Thank you, I made the vowel rule work! and what about for the ones with constant how would I rearrange it like this if the word is: pumpkin
it has to be umpkinpay

because the rules are "If the word starts with a consonant, move the first consonant to the end of the word, then append “ay""
I would leave the original string as is and construct the pig latin string from it. I would use the substr function to extract from the second character onwards and place that in the new string. Then, I would append the first character and "ay" to the end of that new string.
Last edited on
closed account (LN3RX9L8)
@schacktar thats where I am a bit lost. So I use the function and place the string in it?
1
2
3
4
5
6
7
8
9
10
11
getline(infile,line);

//pass appropriate parameters to substr to get line starting at the second character
std::string pigLatin = line.substr( ... );

pigLatin += line[0]; //append the first character (the consonant)

//append "ay"
//...

std::cout << pigLatin << std::endl;
closed account (LN3RX9L8)
Thank you
Topic archived. No new replies allowed.