- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
package bytestring;
public class Main {
public static void main(String[] args) {
String source = new String("A ya sdelal etu hren s perevorotom stroki s ispolzovaniem bayta");
byte bytes[] = source.getBytes();
bytes = reverse(bytes);
String destination = new String(bytes);
System.out.println(destination);
}
private static void switchBytes(byte[] array, int a, int b) {
byte t = array[a];
array[a] = array[b];
array[b] = t;
}
public static byte[] reverse(byte[] bytes) {
int i, j;
int first, last;
int length = bytes.length;
//Переворачиваем всю строку
for(i = 0; i < length / 2; i++)
switchBytes(bytes, i, length - i - 1);
//Переворачиваем каждое слово строки
first = 0;
for(i = 1; i <= length; i++)
if(i == length || bytes[i] == ' ') {
last = i - 1;
for(j = first; j <= first + (last - first) / 2; j++)
switchBytes(bytes, j, first + last - j);
first = i + 1;
}
return bytes;
}
}