최신판 |
당신의 편집 |
1번째 줄: |
1번째 줄: |
| [[분류: 회문]]
| |
| [[분류: 문자열]] | | [[분류: 문자열]] |
| {{DISPLAYTITLE:함수 is_palindrome()}} | | {{DISPLAYTITLE:함수 is_palindrome()}} |
| ==개요==
| |
| ;함수 is_palindrome() | | ;함수 is_palindrome() |
|
| |
|
| * madam → true
| | ==테스트셋== |
| * abradacadarba → true | | * abradacadarba → true |
| * java → false
| |
| * hello → false | | * hello → false |
|
| |
| ==C++==
| |
| [[분류: C++]]
| |
| <syntaxhighlight lang='cpp'>
| |
| #include <bits/stdc++.h>
| |
| using namespace std;
| |
|
| |
| bool isPalindrome(string s) {
| |
| string p = s;
| |
| reverse(p.begin(), p.end());
| |
| return p == s;
| |
| }
| |
|
| |
| int main() {
| |
| // 1
| |
| cout << isPalindrome("kayak") << endl;
| |
| cout << isPalindrome("madam") << endl;
| |
| cout << isPalindrome("racecar") << endl;
| |
| cout << isPalindrome("abradacadarba") << endl;
| |
|
| |
| // 0
| |
| cout << isPalindrome("토마토") << endl;
| |
| cout << isPalindrome("java") << endl;
| |
| cout << isPalindrome("hello") << endl;
| |
| }
| |
| </syntaxhighlight>
| |
|
| |
| ==Java==
| |
| [[분류: Java]]
| |
| {{참고|자바 isPalindrome()}}
| |
| <syntaxhighlight lang='java'>
| |
| public class MyClass {
| |
| static boolean isPalindrome(String s) {
| |
| return s.equals(new StringBuilder(s).reverse().toString());
| |
| }
| |
| public static void main(String args[]) {
| |
| // true
| |
| System.out.println( isPalindrome("kayak") );
| |
| System.out.println( isPalindrome("madam") );
| |
| System.out.println( isPalindrome("racecar") );
| |
| System.out.println( isPalindrome("abradacadarba") );
| |
| System.out.println( isPalindrome("토마토") );
| |
| // false
| |
| System.out.println( isPalindrome("java") );
| |
| System.out.println( isPalindrome("hello") );
| |
| }
| |
| }
| |
| </syntaxhighlight>
| |
|
| |
|
| ==PHP== | | ==PHP== |
| [[분류: PHP]] | | [[분류: PHP]] |
| {{참고|PHP is_palindrome()}}
| | <source lang='php'> |
| <syntaxhighlight lang='php'> | |
| function is_palindrome($str) { return $str == strrev($str); } | | function is_palindrome($str) { return $str == strrev($str); } |
|
| |
|
68번째 줄: |
16번째 줄: |
| var_dump( is_palindrome('hello') ); | | var_dump( is_palindrome('hello') ); |
| # bool(false) | | # bool(false) |
| </syntaxhighlight> | | </source> |
| <syntaxhighlight lang='php'>
| |
| function strrev8($str){
| |
| preg_match_all('/./us', $str, $ar);
| |
| return join('', array_reverse($ar[0]));
| |
| }
| |
| function is_palindrome($str) { return $str == strrev8($str); }
| |
| | |
| # bool(true)
| |
| var_dump( is_palindrome('kayak') );
| |
| var_dump( is_palindrome('madam') );
| |
| var_dump( is_palindrome('racecar') );
| |
| var_dump( is_palindrome('abradacadarba') );
| |
| var_dump( is_palindrome('토마토') );
| |
| | |
| # bool(false)
| |
| var_dump( is_palindrome('hello') );
| |
| var_dump( is_palindrome('coffee') );
| |
| </syntaxhighlight>
| |
| | |
| ==Python==
| |
| [[분류: Python]]
| |
| {{참고|파이썬 is_palindrome()}}
| |
| <syntaxhighlight lang='python'>
| |
| def is_palindrome(word):
| |
| return word[::-1]==word
| |
| | |
| # True
| |
| print( is_palindrome("kayak") )
| |
| print( is_palindrome("madam") )
| |
| print( is_palindrome("racecar") )
| |
| print( is_palindrome("abradacadarba") )
| |
| print( is_palindrome("토마토") )
| |
| | |
| # False
| |
| print( is_palindrome('hello') )
| |
| print( is_palindrome('coffee') )
| |
| </syntaxhighlight>
| |
|
| |
|
| ==같이 보기== | | ==같이 보기== |
| * [[함수 strrev()]] | | * [[함수 strrev()]] |
| * [[함수 is_anagram()]]
| |
| * [[회문]] | | * [[회문]] |