Jump to content

Recommended Posts

Well, foreach is easy to do using a for loop, but only for enumerated arrays. foreach is a language construct too though, so you can't really implement it yourself.

 

The string and array functions should be possible without too much trouble though. You could try str_rot13, str_replace, trim or perhaps ucwords.

Gah...  I'm a slow poster...  People have posted twice since this ;p.

 

 

isset is a language construct.

 

 

Hrmmm, I'm not sure how you could get around using isset though without causing PHP warnings though.

 

 

 

 

 

 

You tried to implement foreach() in C++?  Man, you live on the edge!  How would you have done that?  Make an array_walk type function?  Passing pointers to functions into functions would get murky quickly due to the strictness of function pointer passing in C++, although I guess you could use templating to make that easier maybe.

 

Or do you mean you actually tried to recreate the language construct foreach in C++?

 

 

I actually like the datatypes in C++.  I mean, when it comes down to coding speed, I code in PHP about 2938908923589234 times faster, but something about the rigidity and what not I like.

<?php
function rot13_2($str)
{
	$alpha = array(
	'a',
	'b',
	'c',
	'd',
	'e',
	'f',
	'g',
	'h',
	'i',
	'j',
	'k',
	'l',
	'm',
	'n',
	'o',
	'p',
	'q',
	'r',
	's',
	't',
	'u',
	'v',
	'w',
	'x',
	'y',
	'z'
	);

	$buffer = null;

	for($i = 0; isset($str[$i]); $i++)
	{
		$alphanumeric = false;
		$charcode = 0;

		foreach($alpha as $key => $value)
		{
			if($value == $str[$i])
			{
				$alphanumeric = true;
				$charcode = $key;
			}
		}

		if($alphanumeric)
		{
			$charcode = ($charcode <= 13) ? $charcode + 13 : $charcode - 13;
			$buffer .= $alpha[$charcode];
		}
		else
		{
			$buffer .= $str[$i];
		}
	}

	return $buffer;
}

$str = rot13_2('hello');
echo $str.'<br />';
echo rot13_2($str);
?>

 

str_rot13 :D Proud of myself lol.

 

And yeah...numeric indexes would be easy for foreach. However...not if the indexes don't increment by 1 :P

 

For example, array(0 => 'something', 5 => 'something else); would be pretty hard to do...

 

And yeah...I tried. And failed lol. Oh wait no, it was some sort of implode function. I can't find it now...I had another go though....

#include <iostream>

using namespace std;

string implode(string array)
{
    string buffer;

    int count = sizeof(array) / typeid(array[0]);

    for(int i = 0; i < count; i++)
    {
        buffer += array[i];
    }

    return buffer;
}

int main()
{
    char* array[3] = {"a", "b", "c"};

    cout << implode(array);

    return 0;
}

Does not work at all lol.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.