PHP |给定字符串中的不同字符

我们有string str,我们需要从给定字符串中找到包含所有唯一字符的字符串。所有不同的字符都应按升序打印。

null

例如:

Input : $str = "GeeksforGeeks"
Output : Gefkors
Explanation, the unique characters in the given
string $str in ascending order are G, e, f, k, 
o, r and s

Input : $str = "Computer Science Portal"
Output : CPSaceilmnoprtu
Explanation, the unique characters in the given 
string $str are C, P, S, a, c, ei, l, m, n, o, 
p, r, t and u

这个问题可以通过使用 PHP 内置函数,用于收集给定字符串中使用的所有唯一字符。用于给定问题的内置函数是:

  1. count_chars(): 内置函数的参数包含一个整数类型的返回模式,例如0、1、2、3、4,其中返回模式3以升序返回给定字符串中使用的所有不同字符的字符串。

    注: 字符串区分大小写。

例1:

<?php
// PHP code to find string containing unique
// characters in the given string
$str = "Geeksforgeeks" ;
$result = ( count_chars ( $str , 3));
echo ( $result );
?>


输出:

Gefgkors

例2:

<?php
// PHP code to find string containing unique
// characters  in the given string
$str = "GeeksforGeeks" ;
$result = ( count_chars ( $str , 3));
echo ( $result );
?>


输出:

Gefkors

例3:

<?php
// PHP code to find string containing unique
// characters in the given string
$str = "Computer Science Portal" ;
$result = ( count_chars ( $str , 3));
echo ( $result );
?>


输出:

CPSaceilmnoprtu

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享