본문 바로가기
Language/Java 예제

[Java예제] 문자열 역순 출력, 역순정렬

by 나비와꽃기린 2016. 10. 19.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

public static void main(String[] args) {

	  char[] s={'j','a','v','a','p','r','o','g','r','a','m'};
	  System.out.println(reverseString(s,4,7));
	  System.out.println(reverseString(s,0,3));
	  System.out.println(reverseString(s,8,10));
	  System.out.println(reverseString(s,0,10));

}



public static char[] reverseString(char[] s,int start,int end){
	  char[] reverse=s.clone();
	  char temp;
	  for(int i=start; start<end; i++){
	   temp=reverse[start];
	   reverse[start++]=reverse[end];
	   reverse[end--]=temp;
	  }
	  return reverse;
	 }

<< output >>

javagorpram

avajprogram

javaprogmar

margorpavaj