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 8290787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:54:48+00:00 2026-06-08T12:54:48+00:00

I am trying to form an acronym from a given text. The Idea here

  • 0

I am trying to form an acronym from a given text. The Idea here is that the first Letter in $text ($text[0]) will be taken and placed inside the array $storage using array_push(). Now, if there is a space inside the array, the letter of the next index should be a part of the Acronym. I am currently not getting an ouput, what am I missing?

public function Acronym($text)
        {
            $text = str_split($text);
            $count = strlen($text);
            $storage = array();

            for($i=0; $i<$count; $i++)
            {
                array_push($storage, $text[0]);

                if($text[$i]==' ')
                {
                    array_push($storage, $text[$i+1]);
                }

                foreach($storage as $clean)
                {
                    echo $clean;
                }       
            }   
        }
  • 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-06-08T12:54:49+00:00Added an answer on June 8, 2026 at 12:54 pm

    Your algorithm suffers from a few fatal flaws:

    1. You’re calling strlen() on an array, when you should be calling count():

      $text = str_split($text);
      $count = count($text);
      
    2. However, you can index strings as arrays, so you don’t need str_split() in this scenario, and you can keep $count = strlen( $text); by removing the call to str_split().

    3. This should only happen once, so it should be outside the loop (This implies starting $i at 1):

      array_push($storage, $text[0]);
      
    4. Your foreach loop that prints the $storage array should be outside of the loop that is creating the acronym.

    5. You can save the overhead of calling a function by using the shorthand array_push() notation. You should use array_push() when adding more than one element to an array. Otherwise, this will suffice:

      $storage[] = $text[0];
      
    6. You need to return something from your function, otherwise you won’t be able to access anything outside of it.

    Put that all together, and you get this:

    public function Acronym($text)
    {
        $count = strlen( $text);
    
        $storage[] = $text[0];
    
        for( $i = 1; $i < $count; $i++)
        {
            if( $text[$i] == ' ') 
            {
                $storage[] = $text[$i+1]);
                $i++; // Can increment $i here because we know the next character isn't a space
            }
        }
        foreach($storage as $clean)
        {
            echo $clean;
        }
        return $storage;
    }
    

    That being said, there are far better implementations for forming an acronym giving a string input. Here is one that I can think of:

    public function Acronym( $text) 
    {
        $acronym = array();
        foreach( explode( ' ', $text) as $word)
        {
            $word = trim( $word);
            $acronym[] = strtoupper( $word[0]);
        }
        return implode( '', $acronym);
    }
    

    Note that both functions will fail for inputs like Hello World. I am leaving it up to the OP to make these modifications (if necessary).

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

Sidebar

Related Questions

I'm trying to form a query string from multiple checkboxes that will be used
I got stuck. I am trying to form a function that will eat classless
I am trying to validate form data from server-side. my interest is that the
I'm trying to form a regex that will match a string that looks like
I am trying to intercept form submits from webpages I dont control. My current
I am getting a FaultEvent when trying to send form fields through HTTPService that
I'm trying to form a query that returns a list of entities within a
I'm trying to use form validation that is working in this example with my
Given the table snippet: id | name | age I am trying to form
Im trying my first form validation with PHP. I need some guidance with the

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.