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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:26:31+00:00 2026-05-23T07:26:31+00:00

I take a sentence as input like this: abcd 01234 87 01235 Next, I

  • 0

I take a sentence as input like this:

abcd 01234 87 01235

Next, I have to check every word to see if its characters are consecutive in the alphabet. The output looks like this:

abcd 01234

Well, 01235 contains consecutive chars, but the whole word ALSO contains non-consecutive chars (35), so it’s not printed on the screen.

So far I wrote this:

function string_to_ascii($string)
{
    $ascii = NULL;

    for ($i = 0; $i < strlen($string); $i++)
    {
        $ascii[] =  ord($string[$i]);
    }

    return($ascii);
}


$input = "abcd 01234 87 01235";
//first, we split the sentence into separate words
$input = explode(" ",$input);
foreach($input as $original_word)
{
    //we need it clear
    unset($current_word);

    //convert current word into array of ascii chars
    $ascii_array = string_to_ascii($original_word);

    //needed for counting how many chars are already processed
    $i = 0;

    //we also need to count the total number chars in array
    $ascii_count = count($ascii_array);

     //here we go, checking each character from array
     foreach ($ascii_array as $char)
     {
        //if IT'S THE LAST WORD'S CHAR
        if($i+1 == $ascii_count)
        {
            //IF THE WORD HAS JUST 1 char, output it
            if($ascii_count == 1)
            {
                $current_word  .= chr($char);
            }
            //IF THE WORDS HAS MORE THAN 1 CHAR
            else
            {
                //IF PREVIOUS CHAR CODE IS (CURRENT_CHAR-1)  (CONSECUTIVE, OUTPUT IT)
                if(($char - 1) == $ascii_array[($i-1)])
                {
                    $current_word .=chr($char);
                }

            }
        }
        //IF WE AREN'T YET AT THE ENDING
        else
        {
            //IF NEXT CHAR CODE IS (CURRENT_CHAR+1) (CONSECUTIVE, OUTPUT IT)
            if(($char + 1) == ($ascii_array[($i+1)]))
            {
                $current_word .=chr($char);
            }

        }

        $i++;
     }

    //FINALLY, WE CHECK IF THE TOTAL NUMBER OF CONSECUTIVE CHARS is the same as THE NUMBER OF CHARS
    if(strlen($current_word) == strlen($original_word))
    {
        $output[] = $current_word;
    }

}
//FORMAT IT BACK AS SENTENCE
print(implode(' ',$output));

But maybe there is another way to do this, more simple?

sorry for bad spelling

  • 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-23T07:26:32+00:00Added an answer on May 23, 2026 at 7:26 am

    This works…

    $str = 'abcd 01234 87 01235';
    
    $words = explode(' ', $str);
    
    foreach($words as $key => $word) {
        if ($word != implode(range($word[0], chr(ord($word[0]) + strlen($word) - 1)))) {
           unset($words[$key]);
        }
    }
    
    echo implode(' ', $words);
    

    CodePad.

    Basically, it grabs the first character of each word, and creates the range of characters which would be the value if the word consisted of sequential characters.

    It then does a simple string comparison.

    For a more performant version…

    $str = 'abcd 01234 87 01235';
    
    $words = explode(' ', $str);
    
    foreach($words as $key => $word) {
    
        foreach(str_split($word) as $index => $char) {
          $thisOrd = ord($char); 
          if ($index > 0 AND $thisOrd !== $lastOrd + 1) {
             unset($words[$key]);
             break;
          }
          $lastOrd = $thisOrd;
        }
    
    }
    
    echo implode(' ', $words);
    

    CodePad.

    Both these examples rely on the ordinals of the characters being sequential for sequential characters. This is the case in ASCII, but I am not sure about other characters.

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

Sidebar

Related Questions

I would like to take the words of a sentence, if that word has
Take a look at the ssl_requirement plugin. Shouldn't it check to see if you're
Is there a regex that would take the following sentence: I want this split
So I want to take the words in a sentence and flip the word,
I have a problem. In my project, I take sentence line by line from
I need to find a way to take a sentence and remove all its
I need to take a sentence in that is all on one line with
Take this for example: http://www.haskell.org/haskellwiki/99_questions/Solutions/32 (**) Determine the greatest common divisor of two positive
Take this POJO as the domain. public class Invoice { private String codeNumber; private
Take this example: string1 = aaxadfxasdf string2 = asdfxadf string3 = sadfas i need

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.