Character shift

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
vibes530
Forum Newbie
Posts: 1
Joined: Thu Jun 04, 2009 11:06 am

Character shift

Post 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";
 
 
?>
Last edited by Benjamin on Sun Jun 07, 2009 11:47 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Character shift

Post 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.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: Character shift

Post 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:
Post Reply