- class StringTest
- {
- public static void main(String[] args)
- {
- String s="abcde";
- sop("("+s+")");
- sop("("+reverseString(s)+")");
- System.out.println("Hello World!");
- }
- //将字符串反转
- /*
- 变成数组,对数组反转,变成字符串
- */
- public static String reverseString(String s,int start,int end)
- {
- char[] chs=s.toCharArray();
- reverse(chs,start,end);
- return new String(chs);
- }
- public static String reverseString(String s)
- {
- return reverseString(s,0,s.length());//调用了上面的函数
- }
- private static void reverse(char[] arr,int x,int y)
- {
- for(int start=x,end=y-1;start<end;start++,end--)
- {
- swap(arr,start,end);
- }
- }
- private static void swap(char[] arr,int x,int y)
- {
- char temp=arr[x];
- arr[x]=arr[y];
- arr[y]=temp;
- }
- public static void sop(String obj)
- {
- System.out.println(obj);
- }
- }
复制代码 |
|