检查单词是否由连续字母字符组成

发布于 11-16 12:30 字数 2222 浏览 3 评论 0原文

我将一个句子作为输入,如下所示:

abcd 01234 87 01235

接下来,我必须检查每个单词,看看它的字符在字母表中是否连续。输出如下所示:

abcd 01234

嗯,01235 包含连续字符,但整个单词也包含非连续字符 (35),所以它不会打印在屏幕上。

到目前为止我写了这个:

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));

但也许还有另一种方法可以做到这一点,更简单?

抱歉拼写错误

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋檐2024-11-23 12:30:42

这有效...

$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

基本上,它获取每个单词的第一个字符,并创建字符范围,如果单词由连续字符组成,则该范围将是值。

然后它会进行简单的字符串比较。

如需性能更高的版本...

$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

这两个示例都依赖于连续字符的字符序数。 ASCII 是这样,但其他字符我不确定。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文