Today we had the weekly check-up on the programming course to see if we had done all the assignments and unfortunately hadn’t done one of the assignments because it was added mid-lecture so I guess I was too focused on my own code to realize there was a new assignment to do. But now after about 30 min of coding I’m done with it! Yay for me!
The assignment was to let the user input a string of text and the let the program translate it to “rövarspråk”, I think it’s called robbers language in English. but anyway I tried to keep my main as empty as possible and then doing all the work in a function. I got a little feedback today at the gather up that I needed to put things into loops instead of repeating myself. So in my function translate() I both had two for-loops that were checking if the characters in the string were a consonant or a vowel. I also coded so the program would allow spaces. and in these loops the program also printed out the translated text.
Ulf Benjaminsson said:
This seems like a fun assignment. One thought though:
– Your wovels array only have 6 indices, but the inner loop goes through 20. If the input string contains anything not covered by your arrays (åäö, accents, hypenation or special chars), you go out of bounds.
I would probably avoid iteration and use hashmaps for constant speed access. Like so:
– Set up a a map, using each letter as keys
– Set vowels to 1, consonants to 2
Now you only have to iterate once over the input string, and check what the hashmap holds for that key. Basically;
//loop over your translate string
char c = translate[i];
if(characterMap.find(c) == characterMap.end()){
continue; //invalid input, so go back to top of loop
}
if (mymap[c] > 1){
// consonant, do your stuff
continue; //go back to top of loop
}
// vowel, just output
This should be much much faster than iteration.
slowpork said:
Oh snap! That’s awesome 🙂