G
Guest
Gast
Hallo!
Ich habe ein Programm geschrieben, das mir Zeichenarrays invertiert. Klappt ja auch alles super, aber wie mach ich das, dass auch die Leerzeichen invertiert werden??? :?:
Hab' mal meinen ganzen Code reingestellt; danke schon mal für eure Hilfe!! LG
Ich habe ein Programm geschrieben, das mir Zeichenarrays invertiert. Klappt ja auch alles super, aber wie mach ich das, dass auch die Leerzeichen invertiert werden??? :?:
Hab' mal meinen ganzen Code reingestellt; danke schon mal für eure Hilfe!! LG
Code:
import inout.*;
public class ArrayInvert {
public static void main(String args[]) {
char choice;
do {
char[] chars = readLetters();
Out.print(" Zeichenarray (initial) : ");
printArray(chars);
Out.print(" Zeichenarray (invertiert): ");
invert(chars);
printArray(chars);
// ending or not
do {
Out.print(" Wollen Sie noch ein Array einlesen (n /j)? ");
choice = In.readChar();
Out.println();
} while (!In.done() || (choice != 'n')&& (choice != 'j'));
/*if the input is another thing than "n" or "j", the program
ask one more time "Weitere Berechnung?"*/
} while (choice == 'j');
}
// method for appoint length of an array and read letters of an array
static char[] readLetters() {
Out.print( " Arraygroesze: ");
int bigness = In.readInt();
char[] chars = new char[bigness];
Out.print(" Buchstaben des Arrays: ");
for (int digits = 0; digits < bigness; digits++) {
chars[digits] = In.readChar();
} return chars;
}
// Invert the array chars (in place)
static void invert(char[] chars) {
int i = 0, j = chars.length - 1;
while (i < j) {
char h = chars[i];
chars[i] = chars[j];
chars[j] = h;
i++;
j--;
}
}
// method for printing an array a
static void printArray(char[] chars) {
for (int i = 0; i < chars.length; i++) {
Out.print(chars[i]);
}
Out.println();
}
}