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

 
(사용자 2명의 중간 판 35개는 보이지 않습니다)
9번째 줄: 9번째 줄:
==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
<syntaxhighlight lang='bash' run>
STR=`echo "hello" | rev`
STR=$(echo "hello" | rev)
echo $STR
echo $STR
# olleh
</syntaxhighlight>
</source>
 
==C==
[[분류: C]]
{{참고|C언어 문자열 뒤집기}}
<syntaxhighlight lang='c' run>
#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
}
</syntaxhighlight>
 
==C++==
[[분류: C++]]
{{참고|C++ 문자열 뒤집기}}
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    string s = "hello";
    reverse(s.begin(), s.end());
    cout << s; // olleh
}
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang='csharp'>
{{참고|C샵 문자열 뒤집기|l1=C# 문자열 뒤집기}}
public static string str_reverse(string str)
<syntaxhighlight lang='csharp' run>
using System;
class Program
{
{
  char[] arr = str.ToCharArray();
    static string str_reverse(string str) {
  Array.Reverse(arr);
        char[] arr = str.ToCharArray();
  return new string(arr);
        Array.Reverse(arr);
        return new string(arr);
    }
    static void Main() {
        Console.Write(str_reverse("hello"));
    }
}
}
</source>
</syntaxhighlight>


==Excel==
==Excel==
[[category: Excel]]
[[category: Excel]]
*[[using VBA]]
*[[using VBA]]
<source lang='vb'>
<syntaxhighlight lang='vb'>
Option Explicit
Option Explicit
Public Function str_reverse(str As String)
Public Function str_reverse(str As String)
     str_reverse = StrReverse(str)
     str_reverse = StrReverse(str)
End Function
End Function
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
=str_reverse("hello")
=str_reverse("hello")
</source>
</syntaxhighlight>


==Java==
==Java==
[[category: Java]]
[[category: Java]]
<source lang="java">
{{참고|자바 StringReverse()}}
String reverse = new StringBuilder("hello").reverse().toString();
<syntaxhighlight lang="java" run>
// returns "olleh"
public class HelloWorld {
</source>
    public static void main(String [] args) {
        String s = "hello";
        String reversed = new StringBuilder(s).reverse().toString();
        System.out.println(reversed); // olleh
    }
}
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[Category:JavaScript]]
[[Category:JavaScript]]
<source lang='javascript'>
<syntaxhighlight lang='javascript' run>
function reverse(s) {
function reverse(s) {
   var o = '';
   var o = '';
54번째 줄: 99번째 줄:
   return o;
   return o;
}
}
</source>
console.log( reverse('hello') ); // olleh
<source lang='javascript'>
console.log( reverse('A가☆あ中!') ); // !中あ☆가A
function reverse(s){
</syntaxhighlight>
    return s.split("").reverse().join("");
<syntaxhighlight lang='javascript' run>
}
function reverse(s){ return s.split("").reverse().join(""); }
document.write( reverse("A가☆あ中!") );
console.log( reverse('hello') ); // olleh
// !中あ☆가A
console.log( reverse('A가☆あ中!') ); // !中あ☆가A
</source>
</syntaxhighlight>
<source lang='javascript'>
<syntaxhighlight lang='javascript' run>
String.prototype.reverse=function(){return this.split("").reverse().join("");}
String.prototype.reverse = function(){return this.split("").reverse().join("");}
</source>
console.log( 'hello'.reverse() ); // olleh
console.log( 'A가☆あ中!'.reverse() ); // !中あ☆가A
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang="perl">
<syntaxhighlight lang="perl">
reverse "hello"              # returns "olleh"
reverse "hello"              # returns "olleh"
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
;strrev
{{소스헤더|[[PHP strrev()]]}}
<source lang="php">
<syntaxhighlight lang="php" run>
strrev("hello");            // returns "olleh"
</source>
<source lang="php">
echo strrev("hello"); // olleh
echo strrev("hello"); // olleh
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php' run>
echo strrev("A가☆あ中!");
echo strrev("A가☆あ中!"); // !��䂁㆘‰�A
// !��䂁㆘‰�A
</syntaxhighlight>
echo strrev('Iñtërnâtiônàlizætiøn');
<syntaxhighlight lang='php' run>
// n��it��zil��n��it��nr��t��I
echo strrev('Iñtërnâtiônàlizætiøn'); // n��it��zil��n��it��nr��t��I
</source>
</syntaxhighlight>


;utf8_strrev
{{소스헤더|[[PHP strrev8()]]}}
{{참고|utf8_strrev}}
<syntaxhighlight lang="php" run>
<source lang="php">
function strrev8($str) {
function utf8_strrev($str) {
     return iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
     return iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
}
}
echo utf8_strrev("A가☆あ中!");
echo strrev8("A가☆あ中!") . "\n"; // !中あ☆가A
// !中あ☆가A
echo strrev8('Iñtërnâtiônàlizætiøn') . "\n"; // nøitæzilànôitânrëtñI
echo utf8_strrev('Iñtërnâtiônàlizætiøn');
</syntaxhighlight>
// nøitæzilànôitânrëtñI
</source>


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


print( reverse('hello') )
print( reverse('hello') )
</source>
</syntaxhighlight>
 
==R==
[[분류: R]]
<syntaxhighlight lang='r'>
paste(rev(unlist(strsplit("hello",""))),collapse="")
## [1] "olleh"
paste(rev(unlist(strsplit("A가☆あ中!",""))),collapse="")
## [1] "!中あ☆가A"
</syntaxhighlight>
<syntaxhighlight lang='r'>
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"
</syntaxhighlight>
<syntaxhighlight lang='r'>
library(stringi)
stri_reverse("hello")
## [1] "olleh"
stri_reverse("A가☆あ中!")
## [1] "!中あ☆가A"
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang="ruby">
<syntaxhighlight lang="ruby" run>
print "hello".reverse
print "hello".reverse # olleh
# olleh
</syntaxhighlight>
</source>


==Scheme==
==Scheme==
[[category: Scheme]]
[[category: Scheme]]
<source lang="scheme">
<syntaxhighlight lang="scheme">
(use-modules (srfi srfi-13))
(use-modules (srfi srfi-13))
(string-reverse "hello")    ; returns "olleh"
(string-reverse "hello")    ; returns "olleh"
</source>
</syntaxhighlight>


==SQL==
==SQL==
134번째 줄: 199번째 줄:
===MySQL===
===MySQL===
[[category: MySQL]]
[[category: MySQL]]
<source lang='sql'>
<syntaxhighlight lang='sql'>
SELECT REVERSE( "hello world" );
SELECT REVERSE( "hello world" );
-- dlrow olleh
-- dlrow olleh
</source>
</syntaxhighlight>


==VB==
==VB==
[[category: VB]]
[[category: VB]]
<source lang='vb'>
<syntaxhighlight lang='vb'>
str = StrReverse("hello")
str = StrReverse("hello")
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[함수 strlen()]]
* [[함수 strlen()]]
* [[함수 array_reverse()]]
* [[함수 array_reverse()]]
* [[함수 StringRotate()]]
* [[함수 is_palindrome()‎‎]]
* [[함수 is_palindrome()‎‎]]


==참고==
==참고==
*http://eqcode.com/wiki/string_reverse
*http://eqcode.com/wiki/string_reverse

2023년 9월 29일 (금) 16:03 기준 최신판

  다른 뜻에 대해서는 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 <algorithm> 
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;
}
console.log( reverse('hello') ); // olleh
console.log( reverse('A가☆あ中!') ); // !中あ☆가A
function reverse(s){ return s.split("").reverse().join(""); }
console.log( reverse('hello') ); // olleh
console.log( reverse('A가☆あ中!') ); // !中あ☆가A
String.prototype.reverse = function(){return this.split("").reverse().join("");}
console.log( 'hello'.reverse() ); // olleh
console.log( 'A가☆あ中!'.reverse() ); // !中あ☆가A

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 strrev8($str) {
    return iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
}
echo strrev8("A가☆あ中!") . "\n"; // !中あ☆가A
echo strrev8('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 참고[ | ]