class StringTest
{
public static void main(String[] args)
{
String s = " abcd dert ";
MyTrim(s);
System.out.println(reverseString(s));
// System.out.println(s);
}
public static void MyTrim(String str)
{
int start = 0 ,end = str.length()-1;
while(start <= end && str.charAt(start) == ' ')
start++;
while(start <= end && str.charAt(end) == ' ')
end--;
String s = str.substring(start,end+1);
System.out.println(s);
}
public static String reverseString(String str)
{
char[] chs = str.toCharArray();
reverse(chs);
return new String(chs);
}
public static void reverse(char[] arr)
{
for (int start = 0, end = arr.length-1; start < end; start++,end--)
{
swap(arr,start,end);
}
}
public static void swap(char[] arr, int x, int y)
{
char temp = arr[x];
arr[x] = arr[y];
arr[y] = arr[temp];
}
}
|
|