How do I print random letter from my name?


Then print a random character from your name. Use your knowledge of strlen(string), rand(min, max), and substr(string, start, length) to do this.

how ???


I posted my solution here but it was deleted by someone else (probably a mod). I guess, I'm not allowed to post full solutions here.

So, I try to help you by asking: What is your exact problem?

strlen(string): returns the length of the string
rand(min, max): returns a random number between the lower boundminand upper boundmax
substr(string, start, length): returns a sub-string of string starting from start and the length of length

So, get the length of your string. Put this as the upper bound for the random number. For the lower bound of the random number, choose 0. Then you can retrieve a random letter of your string with substr. :slight_smile:


I've got this error all the time, and I don't understand why?:

Oops, try again. The random output you printed from your name should be just one character.

Here is my code:

// Use rand() to print a random number to the screen
$posicion = rand(0,10);
echo $posicion;
$name = "Humberto Villarino";
echo $name;

// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$posicion = rand(0,strlen($name)-1);
$letra = substr($name,$posicion,1);
echo $name;
echo $letra;


^Does this print only one letter or multiple letters?



I have tried simplifying this and printing every component for over four hours and I'm sure I have it right but I get the same "Oops, try again. The random output you printed from your name should be just one character."

Here is a copy of my code

    $name = "peter";
    $max = strlen ("$name") - 1; //Max output including 0
    $rand = rand (0, $max);
    $output = substr ("$name", $rand, 1); //string, start, length
    echo $output;

    // or echo substr ("$name", rand (0, strlen ("$name"-1)), 1);


Any Ideas where im missing it?



@ lett, I find it very disturbing that a community will be created for solution purposes and posted answers will be deleted by possibly, [codecademy - Admin]..Maybe they want y'all to upgrade to pro and this they could do by asking for donations rather than giving the impression of a free service while tricking people to signup{upgrade} for answers.
Good job @ giving steps



Note that the ($name)-1) was given as an example meaning that whatever the length of your string is, the actual total is output less 1 because of 0 index. Meaning strlen(humbletova) = 10, in reality it is 9 not 10. Javascript/PHP starts counting from index 0. Index 0 is regarded as 1.

So, let us begin;

$name = "humbletova"; (you know what this does, right?)
$lengthName = strlen($name); (this puts the length of your $name into $lengthName )
this is where the trick lies and where you obviously got it wrong;
$randm = rand(0,$lengthName); (Now, I see you nodding. Since the total output of the length of your name is inputted into "$lengthName", it means lower 0, higher/total lengthName 9. No additional number needed)
$sString = substr($name, $randm, 1); (In reality, what that means is sString = substr(humbletover, 0-9, 1)). Do you understand? Aww,,,come on! Okay...substr needs a string, start point and the number of characters. What the code above does is, we give the substr function your name as string. random number representing your name and where to start from since rand() anytime it runs outputs a number between 0=h and 9=a and finally, how many character's' should substr output, which is 1.
finally, echo/print $sString;


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.