Page 1 of 1

Character shift

Posted: Thu Jun 04, 2009 11:12 am
by vibes530
Hello,

I've been learning php in this online class but unfortunately I don't get much help if I don't understand something. We've been given this project to create a program that will translate a word into pig latin. It think I've gotten everything except I can't figure out how to shift the consonant at the beginning of the word to the end. Does anyone have any suggestions? Here is what I have so far.

Thanks!!!

Code: Select all

<?
 
 
function word_latin($pig_latin) {
 $consonants = "^h";
 $new_word = ereg_replace($consonants, " ", $pig_latin);
 return $new_word;
}
 
$pre_latin = "hello";
$post_latin = word_latin($pre_latin);
 
echo $post_latin."ay";
 
 
?>

Re: Character shift

Posted: Fri Jun 05, 2009 2:25 am
by prometheuzz
A couple of things:

* you haven't explained what's wrong with your code;
* what do you think "^h" does?
* are you aware that you will need to split at the starting consonant OR consonant cluster?

The first step in solving some problem is being able to do it on paper. So, please explain how your algorithm works exactly. Once you have that down, translate that into code.

Re: Character shift

Posted: Sat Jun 06, 2009 2:36 pm
by jazz090
fyi ereg is being deprecated in php 5.3 and removed as of 6.0 so u might wanna switch to preg_replace :offtopic: