Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:31:24+00:00 2026-05-25T21:31:24+00:00

Here is a question i want to solve Write a function GeneratePassword which accepts

  • 0

Here is a question i want to solve

“Write a function GeneratePassword which accepts two arguments, an integer and a character string consisting of letters (a-z) and digits (0-9).

When GeneratePassword(5,’abc0123′) is called, it should return a random string of 5 characters taken from ‘abc0123’.

For Example : GeneratePassword(7,’abczxc012394′) could return any of the following outputs :
2c00acb
2c23z93
030b2a4
“

I have written the following code for it, but the system says that the answer is wrong. Please explain what am i doing wrong.

My code :

    function GeneratePassword($digits,$passwordString){
    if($digits > strlen($passwordString)) return '';
    else {
        $randomSelect = '';
        for($i=0;$i<$digits;$i++){
            $randomChar = $passwordString[rand(0, strlen($passwordString)-1)];
            $randomSelect = $randomSelect.$randomChar ;
            $passwordString = str_replace($randomChar,"",$passwordString);

        }
        return $randomSelect;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:31:24+00:00Added an answer on May 25, 2026 at 9:31 pm

    Firstly, you shouldn’t reject $digits being higher than the number of characters to choose from. You could easily receive GeneratePassword(10,'A'), in which case you’d need to return AAAAAAAAAA.

    Second, you shouldn’t remove selected characters from the string. The examples even include duplicate characters (2c00acb for instance).

    Apart from that, your code seems to work. I would suggest, though, that you calculate strlen($passwordString) up front and save it in a variable, rather than computing it once for every iteration of the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something similar to the question here: Grouping Views, Controllers, Models
Taking a look at my question HERE , I now want to return the
This is related to my previous question here . I want 4 divs (absolute
Related question [stackoverflow] here . I'm trying to do the above, but I want
Here's a simple (hopefully) L10N question: Do all locales want this format: Sunday, Nov
Simple question, I just want to select the text from the <Template> tag. Here's
There's already a question on this here. But i want to know if its
C# question here.. I have a UTF-8 string that is being interpreted by a
In basic Java, if I write code to solve a common problem and want
just want to ask anyone here can solve this problem? I want to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.