public class Kumisaapas { /** * AE: (args.length = 3) */ public static void main(String[] args) { char[] swapped = teeSeTemppu(args[0].toCharArray(), args[1].charAt(0), args[2].charAt(0)); for (int i = 0; i < swapped.length; i++) System.out.print(swapped[i]); return; } /** * teeSeTemppu * Metodille annetaan parametreiksi merkkitaulukko ja kaksi merkkimuuttujaa. * Metodi vaihtaa merkkitaulukosta löytyvät parametreiksi annetut merkit * keskenään. * * Esim 1: * char[] str = {'t', 'a', 'u', 'l', 'u', 'k', 'k', 'o'}; * char c1 = 'u'; * char c2 = 'k'; * char[] swapped = teeSeTemppu(str, c1, c2); * for (int i = 0; i < str.length; i++) * System.out.print(swapped[i]); * * Output: * taklkuuo * * AE: t != null * * LE (metodin palauttamaan arvoon viitataan sanalla Result): * (t:n viittaama olio ei muutu) && (Result.length == t.length) * && ( (0 <= i < t.length) : jos (t[i] == y), niin (Result[i] == z) ) * && ( (0 <= i < t.length) : jos (t[i] == z), niin (Result[i] == y) ) * && ( (0 <= i < t.length) : jos (t[i] != y & t[i] != z), * niin (Result[i] == t[i]) ) */ public static char[] teeSeTemppu(char[] t, char y, char z) { char[] result = new char[t.length]; for (int i = 0; i < t.length; i++) { if (t[i] == y) result[i] = z; if (t[i] == z) result[i] = y; if ((t[i] != y) && (t[i] != z)) result[i] = t[i]; } return result; } }