"함수 StringReverse()"의 두 판 사이의 차이

142번째 줄: 142번째 줄:
[[category: Python]]
[[category: Python]]
{{참고|Python string_reverse()}}
{{참고|Python string_reverse()}}
<syntaxhighlight lang="python">
<syntaxhighlight lang="python" run>
print( 'hello'[::-1] )
print( 'hello'[::-1] )
# olleh
# olleh
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='python'>
<syntaxhighlight lang='python' run>
def reverse(text):
def reverse(text):
     result = ""
     result = ""

2021년 9월 24일 (금) 23:24 판

  다른 뜻에 대해서는 array_reverse 문서를 참조하십시오.

1 개요

reverse
StrReverse
strrev
  • 문자열 뒤집기

2 Bash

STR=$(echo "hello" | rev)
echo $STR

3 C

#include <stdio.h>
#include <string.h>
int main() {
    char s[6] = "hello";
    int i, len, temp;
    len = strlen(s);
    for (i=0; i<len/2; i++) {
        temp = s[i];
        s[i] = s[len-i-1];
        s[len-i-1] = temp;
    }
    printf("%s\n", s); // olleh
}

4 C++

#include <iostream>
#include <bits/stdc++.h> 
using namespace std;
int main() {
    string s = "hello"; 
    reverse(s.begin(), s.end()); 
    cout << s; // olleh
}

5 C#

using System;
class Program
{
    static string str_reverse(string str) {
        char[] arr = str.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }
    static void Main() {
        Console.Write(str_reverse("hello"));
    }
}

6 Excel

Option Explicit
Public Function str_reverse(str As String)
    str_reverse = StrReverse(str)
End Function
=str_reverse("hello")

7 Java

public class HelloWorld {
    public static void main(String [] args) {
        String s = "hello";
        String reversed = new StringBuilder(s).reverse().toString();
        System.out.println(reversed); // olleh
    }
}

8 JavaScript

function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--) o += s[i];
  return o;
}
function reverse(s){
    return s.split("").reverse().join("");
}
document.write( reverse("A가☆あ中!") );
// !中あ☆가A
String.prototype.reverse=function(){return this.split("").reverse().join("");}

9 Perl

reverse "hello"              # returns "olleh"

10 PHP

echo strrev("hello"); // olleh
echo strrev("A가☆あ中!"); // !��䂁㆘‰�A
echo strrev('Iñtërnâtiônàlizætiøn'); // n��it��zil��n��it��nr��t��I
function utf8_strrev($str) {
    return iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
}
echo utf8_strrev("A가☆あ中!") . "\n"; // !中あ☆가A
echo utf8_strrev('Iñtërnâtiônàlizætiøn') . "\n"; // nøitæzilànôitânrëtñI

11 Python

print( 'hello'[::-1] )
# olleh
def reverse(text):
    result = ""
    for i in range(0, len(text)):
        result = str(text[i]) + result
    return result

print( reverse('hello') )

12 R

paste(rev(unlist(strsplit("hello",""))),collapse="")
## [1] "olleh"
paste(rev(unlist(strsplit("A가☆あ中!",""))),collapse="")
## [1] "!中あ☆가A"
s = "hello"
paste(substring(s,nchar(s):1,nchar(s):1),collapse="")
## [1] "olleh"
s = "A가☆あ中!"
paste(substring(s,nchar(s):1,nchar(s):1),collapse="")
## [1] "!中あ☆가A"
library(stringi)
stri_reverse("hello")
## [1] "olleh"
stri_reverse("A가☆あ中!")
## [1] "!中あ☆가A"

13 Ruby

print "hello".reverse
# olleh

14 Scheme

(use-modules (srfi srfi-13))
(string-reverse "hello")     ; returns "olleh"

15 SQL

15.1 MySQL

SELECT REVERSE( "hello world" );
-- dlrow olleh

16 VB

str = StrReverse("hello")

17 같이 보기

18 참고