views:

167

answers:

3

I am beginning to write a translator program which will translate a string of text found on a file using parallel arrays. The language to translate is pig Latin. I created a text file to use as a pig latin to English dictionary. I didn't want to use any two dimension arrays; I want to keep the arrays in one dimension. Basically I want to read a text file written in PigLatin and using the dictionary I created I want to output the translation to English on the command line.

My pseudo-code idea is:

  • Open the dictionary text file.

  • Ask the user for the name of the text file written in PigLatin that he/she wants to translate to English

  • Searching each word on the user's text file and comparing to the Dictionary to then translate the word accordingly. Keep on going until there are no more words to translate.

  • Show the translated words on the command line screen.

I was thinking on using a parallel arrays, one containing the english translated words and another one containing the pig latin words.

I would like to know how can I manipulate the strings using arrays in C++?

Thank you.

+4  A: 

Pig latin can be translated on the fly.

Just split the words before the first vowel of each word and you won't need a dictionary file. Then concatenate the second part with the first part, delimited with a '-', and add "ay" at the end.

Unless you want to use a dictionary file?

pbos
I would like to use a dictionary so I can learn string manipulation better. Do you have any ideas of how to achieve that?Thank you for the quick response.
jualin
I'm not sure how a dictionary would help you with learning string manipulation, though? std::map makes a clean dictionary. Not that much educational value in using it though. Wouldn't translating this on the fly teach you string manipulation, am I missing something?
pbos
Thank you I will look into that.
jualin
+5  A: 

If files will be always translated in one direction (e.g. PigLatin -> English) then it would be easier and more efficient to use std::map to map one string to another:

std::map<std::string, std::string> dictionary;
dictionary["ashtray"] = "trash";
dictionary["underplay"] = "plunder";

And get translated word, just use dictionary[] to lookup (e.g. std::cout << dictionary["upidstay"] << std::endl;)

Aleksei Potov
std::map is easily the cleanest way to do a dictionary. :)
pbos
I understand that it is more efficient but my main objective is to learn how to work with parallel arrays. How would I go on using parallel arrays for this.
jualin
+1  A: 

Declaring an array of strings is easy, the same as declaring an array of anything else.

const int MaxWords = 100;
std::string piglatin[MaxWords];

That's an array of 100 string objects, and the array is named piglatin. The strings start out empty. You can fill the array like this:

int numWords = 0;
std::ifstream input("piglatin.txt");
std::string line;
while (std::getline(input, line) && numWords < MaxWords) {
  piglatin[numWords] = line;
  ++numWords;
}
if (numWords == MaxWords) {
  std::cerr << "Too many words" << std::endl;
}


I strongly recommend you not use an array. Use a container object, such as std::vector or std::deque, instead. That way, you can load the contents of the files without knowing in advance how big the files are. With the example declaration above, you need to make sure you don't have more than 100 entries in your file, and if there are fewer than 100, then you need to keep track of how many entries in your array are valid.

std::vector<std::string> piglatin;

std::ifstream input("piglatin.txt");
std::string line;
while (std::getline(input, line)) {
  piglatin.push_back(line);
}
Rob Kennedy
I would like to use an array because I haven't learned about the standard vector yet. Let's say that I know how many words the file will contain. For instance let's say that the file will contain at the most 30 words. The dictionary would have 100 words with their corresponding translations.I understand that I would declare the array with size 30. Is that correct? And then I would read in the words on the user's text file and look for their translation on the dictionary file. Finally I will cout the translation on the command line. Any ideas on how to make that comparison.Thank you.
jualin
Yes. If you know how many words you'll have, then declare an array with that many elements, as I demonstrated. To compare strings for equality, you can use the `==` operator, just like comparing most other types. (Don't use `==` on `char*`; it won't do what you expect. But you're probably not using `char*`, so don't worry about it.)
Rob Kennedy
@jualin: if you're learning C++ specifically, it's actually a good idea to learn about `std::vector` first, and arrays later. Vectors are more high-level. Similarly, you should learn about `std::string` first, and working with raw `char*` strings later.
Pavel Minaev
Thank you both for the idea. I will look into the std: :vector Pavel.Thank you for the quick response.
jualin
The dictionary I am planning on using has pretty much this format:PigLatin word English TranslationTherefore I was thinking that I could read in this file to a parallel array. Is this correct?
jualin
Yes, you can read your words into two arrays. It won't be quite as simple as I made it out to be in my code examples — I thought you had the words in two separate files. You'll need to split the string into two words. Or you can change how you read the file. Instead of `getline`, use the `>>` operator. Your instructor or teaching assistant can tell you more about what you're expected to use for this assignment.
Rob Kennedy