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)
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'
@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.
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;