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

10번째 줄: 10번째 줄:
[[category: Bash]]
[[category: Bash]]
<syntaxhighlight lang='bash' run>
<syntaxhighlight lang='bash' run>
STR=`echo "hello" | rev`
STR=$(echo "hello" | rev)
echo $STR
echo $STR
</syntaxhighlight>
</syntaxhighlight>

2021년 4월 13일 (화) 03:51 판

  다른 뜻에 대해서는 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[5] = "hello";
    int i, j, temp;
    for (i=0, j=strlen(s)-1; i<j; i++, j--) {
        temp = s[i];
        s[i] = s[j];
        s[j] = 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#

public static string str_reverse(string str)
{
  char[] arr = str.ToCharArray();
  Array.Reverse(arr);
  return new string(arr);
}

6 Excel

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

7 Java

String reversed = new StringBuilder("hello").reverse().toString();
// returns "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

strrev
strrev("hello");             // returns "olleh"
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
utf8_strrev
function utf8_strrev($str) {
    return iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
}
echo utf8_strrev("A가☆あ中!");
// !中あ☆가A
echo utf8_strrev('Iñtërnâtiônàlizætiø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 참고